Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.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
C# 如何处理无法连接到远程服务器的WebException?_C#_Winforms - Fatal编程技术网

C# 如何处理无法连接到远程服务器的WebException?

C# 如何处理无法连接到远程服务器的WebException?,c#,winforms,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 StackTra

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

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: 

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

Rooxo我应该将try-and-catch和try-web页面放入计时器中,比如说每60秒或几分钟一次吗?我的意思是一旦在构造函数中尝试并捕获,如果它抛出异常,那么将其移动到计时器,然后打开计时器并在那里尝试并捕获,直到网页工作为止。这是一个好的逻辑吗?如果是这样的话,怎么做呢?我不会一直尝试访问该网站。如果真的有必要修改内容,只需退出时出现错误,并让用户决定何时重试