我应该如何处理WebException无法连接到远程服务器?

时间:2014-02-16 12:28:29

标签: c# winforms

我知道网站现在有一个问题,但我现在和未来应如何处理这个案例?

page = client.DownloadString("http://rotter.net/scoopscache.html");

页面是字符串变量。

System.Net.WebException was unhandled
  HResult=-2146233079
  Message=Unable to connect to the remote server
  Source=System
  StackTrace:
       at System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request)
       at System.Net.WebClient.DownloadString(Uri address)
       at System.Net.WebClient.DownloadString(String address)
       at ScrollLabelTest.Form1..ctor() in e:\scrolllabel\ScrollLabel\ScrollLabel\Form1.cs:line 41
       at ScrollLabelTest.Program.Main() in e:\scrolllabel\ScrollLabel\ScrollLabel\Program.cs:line 18
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: System.Net.Sockets.SocketException
       HResult=-2147467259
       Message=A connection attempt failed because the connected party did not properly respond after a period of time or established connection failed because connected host has failed to respond 199.203.52.152:80
       Source=System
       ErrorCode=10060
       NativeErrorCode=10060
       StackTrace:
            at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
            at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception)
       InnerException: 

1 个答案:

答案 0 :(得分:2)

您应该将请求置于try-catch-condition中,并根据页面的重要程度,使用错误消息停止程序,或者使用空字符串继续,并相应地计划我们的程序

相关问题