Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Delphi 检查internet访问始终返回true_Delphi_Internet Connection - Fatal编程技术网

Delphi 检查internet访问始终返回true

Delphi 检查internet访问始终返回true,delphi,internet-connection,Delphi,Internet Connection,以下代码在我的系统上始终返回True: uses WinInet; function CheckInternetConnection() : Boolean; var dwConnectionTypes: Integer; begin dwConnectionTypes := ( INTERNET_CONNECTION_MODEM + INTERNET_CONNECTION_LAN + INTERNET_CONNECTIO

以下代码在我的系统上始终返回True:

uses
    WinInet;

function CheckInternetConnection() : Boolean;
var
    dwConnectionTypes: Integer;
begin
    dwConnectionTypes := (
        INTERNET_CONNECTION_MODEM +
        INTERNET_CONNECTION_LAN +
        INTERNET_CONNECTION_PROXY);     // "dwConnectionTypes" now "7"
    if (InternetGetConnectedState(@dwConnectionTypes, 0)) then
        Result := True      // Always hit, "dwConnectionTypes" now "18"
    else
        Result := False;    // Never reaches here!
end;
我试过:

* unplugging the network cable
* stopped "Wireless Zero Configuration" service
* disabled all connections in Control Panel > Network Connections
* definitely confirmed no internet connection in a web browser
我错过了什么

更新
我已经确认,动态加载
wininet.dll
并使用GetProcAddress查找方法“InternetGetConnectedState”会在internet断开连接时给出完全相同的结果(返回True,参数设置为“18”)。

如果您想知道是否连接到internet,没有其他方法可以在internet上与主机联系

正确从技术上讲,您只知道该主机是否在线,但这通常足够好,因为如果您的程序需要访问internet,那是因为您需要在internet上扮演主机

一种方法是使用Indy提供的
TIdHTTP

uses
  IdHTTP;
使用 IdHTTP

function HasInternet: Boolean;
begin
  with TIdHTTP.Create(nil) do
    try
      try
        HandleRedirects := True;
        Result := Get('http://www.Google.com/') <> '';
      except
        Result := false;
      end;
    finally
      free;
    end;
end;

但最好还是试着联系你的主人

如果您想知道自己是否已连接到Internet,没有其他方法可以与Internet上的主机联系

正确从技术上讲,您只知道该主机是否在线,但这通常足够好,因为如果您的程序需要访问internet,那是因为您需要在internet上扮演主机

一种方法是使用Indy提供的
TIdHTTP

uses
  IdHTTP;
使用 IdHTTP

function HasInternet: Boolean;
begin
  with TIdHTTP.Create(nil) do
    try
      try
        HandleRedirects := True;
        Result := Get('http://www.Google.com/') <> '';
      except
        Result := false;
      end;
    finally
      free;
    end;
end;


但最好还是试着联系你的主人

测试这一点几乎毫无意义。试着联系一下。如果您无法接通,则表示没有连接。文档中说:InternetGetConnectedState返回值TRUE表示至少有一个到Internet的连接可用。它不保证可以建立到特定主机的连接。请注意,
dwConnectionTypes
的输入值没有意义。该参数仅用于输出。您得到的结果等于
已安装的INTERNET\u连接\u LAN或INTERNET\u RAS\u
。没有“保证可用”的服务器。可能您在没有外部连接的LAN上。也许你在防火墙后面。也许越洋电缆被切断了。也许“保证”服务器刚刚被流星击中。检查是否连接到您需要的任何服务器。除了Rob所说的输出外,它基本上只是告诉您已连接到LAN,并且已安装拨号网络。但是,仅仅因为您连接到局域网,并不保证局域网连接到互联网。因此,您确实需要连接到Internet上的外部服务器以检查是否连接到Internet。测试这一点几乎毫无意义。试着联系一下。如果您无法接通,则表示没有连接。文档中说:InternetGetConnectedState返回值TRUE表示至少有一个到Internet的连接可用。它不保证可以建立到特定主机的连接。请注意,
dwConnectionTypes
的输入值没有意义。该参数仅用于输出。您得到的结果等于
已安装的INTERNET\u连接\u LAN或INTERNET\u RAS\u
。没有“保证可用”的服务器。可能您在没有外部连接的LAN上。也许你在防火墙后面。也许越洋电缆被切断了。也许“保证”服务器刚刚被流星击中。检查是否连接到您需要的任何服务器。除了Rob所说的输出外,它基本上只是告诉您已连接到LAN,并且已安装拨号网络。但是,仅仅因为您连接到局域网,并不保证局域网连接到互联网。因此,您确实需要连接到Internet上的外部服务器,以检查是否连接到Internet。使用该代码,为什么TIdHTTP.Get和TIdHTTP.Trace返回非空字符串(和work),但以下所有操作都会引发异常并失败(注意,我在“http://”中添加了一个空格)要停止将字符串转换为URL,它们在我的代码中是正确的!):http://www.teamviewer.com/、http://www.bbc.co.uk/、http://microsoft.com/。事实上,除了stackoverflow之外,我无法使任何内容正常工作。因为它重定向到https。看我更新的答案。很有趣。这改善了某些服务器的性能(现在www.bbc.co.uk起作用),但对其他服务器则没有改善(www.microsoft.com继续出现故障)。我要测试连接的特定服务器是master14.teamviewer.com…您更新的代码修复了这一问题。很好…但是知道www.microsoft.com为什么继续失败吗?更新:通过在尝试连接到www.microsoft.com时检查异常,我发现引发了EIdHTTPProtocolException。消息是:“Indy引发了一个协议错误!HTTP状态代码:403错误消息HTTP/1.1 403禁止”。有什么想法吗?此异常是否仍然可以归类为“可以有效连接到internet”?确实可以,因为403是从服务器获取的错误代码。尝试禁用internet连接并测试该功能。如果您想在浏览器运行时获得真正的HTML响应,则必须使用代码,为什么TIdHTTP.Get和TIdHTTP.Trace返回非空字符串(并且有效),但以下所有操作都会引发异常并失败(请注意,我在“http://”中添加了一个空格以阻止字符串转换为URL,它们在我的代码中是正确的!):http://www.teamviewer.com/、http://www.bbc.co.uk/、http://microsoft.com/事实上,除了stackoverflow之外,我什么都做不到。因为它重定向到https。看我更新的答案。很有趣。这改善了某些服务器的性能(现在www.bbc.co.uk起作用),但对其他服务器则没有改善(www.microsoft.com继续出现故障)。我要测试连接的特定服务器是master14.teamviewer.com…您更新的代码修复了这一问题。很好…但是你知道为什么www.microsoft.com继续失败吗?更新:在尝试连接到www.micr时检查异常