如何快速偵測目前網路是否可以上網(瀏覽網頁或FTP) |
缺席
|
g9221712
高階會員 發表:145 回覆:344 積分:162 註冊:2006-07-06 發送簡訊給我 |
||||||||||
axsoft
版主 發表:681 回覆:1056 積分:969 註冊:2002-03-13 發送簡訊給我 |
How To Detecting If You Have a Connection to the Internet
資料來源:
function loadTOCNode(){}
This article was previously published under Q242558
SUMMARY
loadTOCNode(1, 'summary');
Many of you want to know if a computer has an active Internet connection before trying to connect to the Internet using the WinInet API or some other communication interface. The internet connection is important if you don't want your application to cause Windows to automatically dial the default Internet modem connection.
This article provides a mechanism for determining if an Internet site is accessible without the risk of you being prompted to dial into another Internet Service Provider. MORE INFORMATION
loadTOCNode(1, 'moreinformation');
Usually the best way to determine if you have a connection to a particular computer is to attempt the connection. If the autodial feature of Windows is enabled then attempting the connection may cause the default Internet dialup connectoid to be opened, and you will be prompted with your credentials to connect.
To avoid having the default Internet connectoid dialed, the InternetGetConnectedState function can be used to determine if there is a default Internet dialup connectoid configured and whether it is currently active or not. If there is a default Internet dialup connectoid configured and it is not currently active then InternetGetConnectedState will return FALSE. If InternetGetConnectedState returns TRUE then you can attempt to connect to the Internet resource without fear of being prompted to connect to another Internet Service Provider. The following code demonstrates how you would do this:
You cannot rely solely on the fact that InternetGetConnectedState returning TRUE means that you have a valid active Internet connection. It is impossible for InternetGetConnectedState to determine if the entire connection to the Internet is functioning without sending a request to a server. This is why you need to send a request to determine if you are really connected or not. You can be assured however that if InternetGetConnectedState returns TRUE, that attempting your connection will NOT cause you to be prompted to connect to the default Internet Service Provider. Be aware that InternetGetConnectedState only reports the status of the default Internet connectoid on Internet Explorer 4.x. If a nondefault connectoid is connected, InternetGetConnectedState always returns FALSE (unless a LAN connection is used). With Internet Explorer 4.x configured to use a LAN connection, InternetGetCo nectedState always returns TRUE. Internet Explorer 5 behaves differently. If you are currently dialed into a different dial-up in Internet Explorer 5, InternetGetConnectedState reports dial-up connection status as long as any connectoid is dialed or an active LAN connection exists. There are some other ways to try to determine if you currently have a connection to a particular network resource. The IsDestinationReachable() function can be used to find out if there is a current connection to an address. However, he IsDestinationReachable() function is only concerned with whether the IP address is reachable from your computer. It does not work through HTTP proxies or firewalls that restrict ICMP ping packets. It is also possible to use RasEnumConnections to enhance your code so that you can tell if there is an active dialup connection that might have Internet access even though it is not the default Internet dialup connectoid. |
|||||||||
axsoft
版主 發表:681 回覆:1056 積分:969 註冊:2002-03-13 發送簡訊給我 |
unit Unit1; interface uses type var implementation {$R *.DFM} procedure TForm1.Button1Click(Sender: TObject); end. |
|||||||||
g9221712
高階會員 發表:145 回覆:344 積分:162 註冊:2006-07-06 發送簡訊給我 |
謝謝您的回覆,你所說得方法可以偵測網路狀態! 但是若於程式執行中拔除網路線,我發現偵測結果會錯誤!但若在斷線狀態時中斷程式然後在進入程式偵測一次
------
「人們所以覺得寂寞,是因為他們會築牆,卻不會搭橋。」 程式寫的越久,卻發現自己越來越不會寫程式! |
|||||||||
pceyes
尊榮會員 發表:70 回覆:657 積分:1140 註冊:2003-03-13 發送簡訊給我 |
uses wininet;
// Get offline state // Alhaiseb Misurata Libya function IsGlobalOffline: Boolean; var State, Size: DWORD; begin Result := False; State := 0; Size := SizeOf(DWORD); if InternetQueryOption(nil, INTERNET_OPTION_CONNECTED_STATE, @State, Size) then if (State and INTERNET_STATE_DISCONNECTED_BY_USER) <> 0 then Result := True; end; //Set offline state procedure SetGlobalOffline(fGoOffline: Boolean); var ci: INTERNET_CONNECTED_INFO; begin if fGoOffline then begin ci.dwConnectedState := INTERNET_STATE_DISCONNECTED_BY_USER; ci.dwFlags := ISO_FORCE_DISCONNECTED; end else ci.dwConnectedState := INTERNET_STATE_CONNECTED; InternetSetOption(nil, INTERNET_OPTION_CONNECTED_STATE, @ci, SizeOf(ci)); end;
------
努力會更接近成功 |
|||||||||
g9221712
高階會員 發表:145 回覆:344 積分:162 註冊:2006-07-06 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |