1

I am using web client to send some request parameters to server and then get response according to request. 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. 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.

Monish Koyott
  • 374
  • 1
  • 4
  • 20
M.Farrukh
  • 45
  • 10
  • You have an answer, but I'd also suggest to switch to the asynchronous method `.UploadValuesAsync`, to detach the UI from the process, and get notifications about the progress of the operation [UploadProgressChanged Event](https://msdn.microsoft.com/en-us/library/system.net.webclient.uploadprogresschanged(v=vs.110).aspx). Also take a look at [NetworkAvailabilityChanged Event](https://msdn.microsoft.com/en-us/library/system.net.networkinformation.networkchange.networkavailabilitychanged.aspx). Subscribe the event to receive a notification when no network connections are available. – Jimi Dec 19 '17 at 07:10
  • @Jimi you give me a suggestion to use '.UploadValuesAsync'. it's work but it return nothing and i want a response in bytes form.According to response we perform different tasks. Have solution for that situation. Thanks. – M.Farrukh Dec 20 '17 at 05:10
  • Subscribe the WebClient event [UploadValuesCompletedEventHandler](https://learn.microsoft.com/en-us/dotnet/api/system.net.uploadvaluescompletedeventhandler?view=netframework-4.7). When the Upload is completed, [UploadValuesCompletedEventArgs](https://learn.microsoft.com/en-us/dotnet/api/system.net.uploadvaluescompletedeventargs?view=netframework-4.7) `e.Result` contains the answer form the server in Byte array. It also returns `e.Error` and `e.Cancelled` (with 2 "L" this time) – Jimi Dec 20 '17 at 14:14

2 Answers2

0

You can extend WebClient, so it will have timeout. 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;
  }
}
Alex Butenko
  • 3,664
  • 3
  • 35
  • 54
  • Sorry i think you misunderstood my question as i don't give much info. Dear my application become hanged for a while and then throw the exception.I want that when ever it process that line of code if internet gets disconnect it will terminate it (If possible). Thanks – M.Farrukh Dec 19 '17 at 07:36
  • I don't really understand. Where is no common way to check your internet connection. If you want exception on timeout - change the sample code accordingly. – Alex Butenko Dec 19 '17 at 07:52
0
 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. You can also use threads for working this code independently your UI will not Hanged.

Hope it will help.

Ahmad
  • 770
  • 9
  • 21