我正在调用这个方法:
ServicePoint sp = ServicePointManager.FindServicePoint(mRequest.RequestUri, this.MapDataWebProxy);用于获取服务点,但当没有可用的internet连接时,该方法就不会返回。
关于如何防止这种情况或设置超时,您有什么建议吗?
发布于 2019-03-14 22:11:27
您可以尝试在调用该方法之前验证Internet连接。它可以像这样做:
[DllImport("wininet.dll")]
private extern static bool InternetGetConnectedState(out int Description, int ReservedValue);
//Creating a function that uses the API function...
public static bool IsConnectedToInternet()
{
int Desc;
return InternetGetConnectedState(out Desc, 0);
}
public ServicePoint GetServicePoint()
{
if (!IsConnectedToInternet())
{
return null;
}
return ServicePointManager.FindServicePoint(mRequest.RequestUri, this.MapDataWebProxy);
}此外,还可以用另一种方式检查互联网。不使用"wininet.dll“库:What is the best way to check for Internet connectivity using .NET?
https://stackoverflow.com/questions/55164207
复制相似问题