您的Internet连接断开时的HTTP代码? [英] HTTP code when your Internet connection drops?
问题描述
当你在网上浏览任何网站时,你会得到好的或坏的状态代码:
例子
200好 - 一切顺利>
404 BAD - 找不到网站
如果我的互联网连接断开,我会得到什么?
是否有针对此类问题的HTTP代码?
如果没有此类代码,那么您可能仍然可以帮助我;)我需要使用C#捕获.NET上的异常。只是请,我们不要去解决方法,我真的需要知道是否没有互联网连接所以我可以处理与404不同的错误。
尝试
{
// HttpWebRequest req = new ...
}
catch ( WebException ex)
{
// 如何连接互联网?
}
非常感谢!!
为什么不作为例外处理尝试
{
/ / HttpWebRequest req = new ...
}
catch (WebException ex)
{
}
catch (例外情况ex)
{
}
你也可以使用
System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()
如果连接可用则返回True否则返回false
这是一篇很好的文章你可以参考
检测Internet网络可用性 [ ^ ]
错误无法解析远程名称:表示网络不可用。
所以请通过禁用网络来尝试此代码。
try
{
WebRequest rc = HttpWebRequest.Create(& quot; http: //www.google.com& quot;);
rc.GetResponse();
}
catch (WebException ex)
{
// 无法解析远程名称:www.google.com错误表示网络不可用
}
When you browse on the Web for any site, you get status codes either for good or for bad:
Examples
200 GOOD - everything went alright
404 BAD - Site not found
What will I get if my Internet connection drops?
Is there an HTTP code for that kind of problem?
If there is no such code, then you can probably still help me ;) I need to catch the exception on .NET with C#. Just please, let's not go for a workaround, I really need to know if there is no Internet connection so I can handle the error different from a 404.
try
{
//HttpWebRequest req = new ...
}
catch (WebException ex)
{
// How do I get no internet connection ?
}
Thank you very much!!
Why not to handle as Exceptiontry { //HttpWebRequest req = new ... } catch (WebException ex) { } catch (Exception ex) { }
You can also use
System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()
Returns True if connection available else it will return false
Here's a nice article you can refer
Detect Internet Network Availability[^]
The Error "The remote name could not be resolved: " is indicating that network unavailability.
So try this code by disabling network.
try { WebRequest rc = HttpWebRequest.Create("http://www.google.com"); rc.GetResponse(); } catch (WebException ex) { //The remote name could not be resolved: 'www.google.com' error is indicating the Network unavailability }
这篇关于您的Internet连接断开时的HTTP代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!