gpt4 book ai didi

c++ - 在 C++ 应用程序 : gethostbyname() failing 中检查互联网连接的可靠方法

转载 作者:可可西里 更新时间:2023-11-01 14:41:27 26 4
gpt4 key购买 nike

我正在 Windows 上开发 C++ 网络应用程序。我需要检查是否存在互联网连接。我正在使用 gethostbyname(),但当系统通过代理连接时,它提供的信息不正确。 InternetCheckConnection()InternetGetConnectedState() 在不同条件下也没有给出可靠的结果。是否有一种可靠的方法来检查涵盖代理和 VPN 等所有条件的互联网连接?

更新:

在我们公司的网络中,WinHttpGetProxyForUrl() 失败并出现错误 ERROR_WINHTTP_AUTODETECTION_FAILED 并且 WinHttpSendrequest() 失败并出现错误 ERROR_WINHTTP_NAME_NOT_RESOLVED.

在开放网络中 WinHttpSendrequest() 成功。

最佳答案

老办法!

包括:

#include <wininet.h>
#pragma comment(lib,"Wininet.lib")

在你的方法中:

char url[128];
strcat(url, "http://www.techtoolbox.com");
bool bConnect = InternetCheckConnection(url, FLAG_ICC_FORCE_CONNECTION, 0);


if (bConnect)
{
//internet connection exists !
}
else
{
//internet DOES NOT connection exists !
}

希望对您有所帮助。

关于c++ - 在 C++ 应用程序 : gethostbyname() failing 中检查互联网连接的可靠方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11752934/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com