简体   繁体   English

如果使用以下命令执行Internet断开连接,Web客户端请求将被挂起

[英]Web client request gets hanged if Internet is disconnected while executing it using

I am using web client to send some request parameters to server and then get response according to request. 我正在使用Web客户端向服务器发送一些请求参数,然后根据请求获得响应。 But the issue which i face when every i try to send web client request is if Internet gets disconnected while sending request, it will hang my application. 但是我每次尝试发送Web客户端请求时都面临的问题是,如果在发送请求时Internet断开连接,它将使我的应用程序挂起。 Sometimes it crashes the application. 有时它会使应用程序崩溃。 My code snippet is give below. 我的代码段如下。

 WebClient webClient=new WebClient()
  userData = webClient.UploadValues(URL, "POST", parameters);

                       `

While executing that line if Internet disconnects it hangs the application. 如果执行该行(如果Internet断开连接),它将挂起应用程序。

You can extend WebClient, so it will have timeout. 您可以扩展WebClient,因此它将超时。 Try this: 尝试这个:

class WebClientWithTimeout : WebClient {
  public WebClientWithTimeout(int timeout) {
    _timeout = timeout
  }
  // Timeout in seconds
  int _timeout;
  protected override WebRequest GetWebRequest(Uri uri) {
    WebRequest webRequest = base.GetWebRequest(uri);
    webRequest.Timeout = 1000 * _timeout;
    return w;
  }
}
 private class YourWebClient : WebClient
        {
            protected override WebRequest GetWebRequest(Uri uri)
            {
                WebRequest wr = base.GetWebRequest(uri);
                wr.Timeout = 7 * 1000;
                return wr;
            }
        }

You can change your timeout accordingly to your desire. 您可以根据需要更改超时时间。 I have tested it and it worked for me. 我已经对其进行了测试,并且对我有用。 Actually webclient default timeout is 100 sec but it takes about 20 to 25 or 30 sec in default.So you change it according to your responce that you know how much time it takes from your server. 实际上, webclient默认超时时间是100秒,但默认情况下大约需要20到25或30秒。因此,您可以根据自己的响应(知道服务器要花费多少时间)进行更改。 You can also use threads for working this code independently your UI will not Hanged. 您也可以使用threads独立处理此代码,而UI不会挂起。

Hope it will help. 希望它会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么主线程在后台工作程序执行时被挂起 - Why main thread gets hanged when background worker is executing 使用后台工作进程时,Windows应用程序被挂起 - Windows Application gets hanged when using a background worker process 使用JQuery添加用户控件时,“对处理程序'System.Web.UI.Page'执行子请求出错。” - “Error executing child request for handler 'System.Web.UI.Page'.” while adding user control using JQuery Ruby on Rails TCPSocket在客户端应用程序中冻结,而循环使用gets - Ruby on Rails TCPSocket freezes in Client Application while loop using gets 客户端断开连接 - The client disconnected 执行异步Web请求时出现问题 - Problem Executing Async Web Request Signalr 从服务器向客户端发送消息 c# 执行请求时发生未处理的异常 - Signalr Sending messages from server to client c# An unhandled exception has occurred while executing the request Web请求在客户端上运行 - Web Request to run on client 在 ASP.Net CORE Web 请求上执行异步工作时将文件保存在磁盘上的最有效方法 - Most efficient way to save a file on disk while executing async work, on a ASP.Net CORE Web Request 如何在WCF服务返回数据之前确定客户端是否断开连接? - How to find out if a client gets disconnected before a WCF service returns data?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM