HttpWebRequest The request was aborted: The operation has timed out

2.2k Views Asked by At

I am trying to upload a long video to jwplatform with the help of HttpWebRequest.I have set the Timeout as below . when i upload data it throw error.

The request was aborted: The operation has timed out

Event though the server timeout is along enough to weight for response but i still returning the same error.

 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(callUrl);
            request.ContentType = "multipart/form-data; boundary=" +
                                    boundary;
            request.Method = "POST";
            request.KeepAlive = true;
            request.Timeout= 24*60*60*1000;
...
            using (var response = request.GetResponse())
            {
                Stream stream2 = response.GetResponseStream();
                StreamReader reader2 = new StreamReader(stream2);
                res = reader2.ReadToEnd();
            }

I am using the GetResponse() mathod as above.

Any help on how i can force the webrequest to wait until the data is fully upload will be highly appreciated. Thanks in Advance.

1

There are 1 best solutions below

0
SarahB On

You can use request.GetResponseAsync (). and so use asynchronous task to wait properly the response.

To help you, it'll be something like this :

public  async Task<string> InfoAnswerAsync(WebRequest request)
 { 
using (var response = await request.GetResponseAsync())
{
    using (var stream = response.GetResponseStream())
        if (stream != null)
            using (var reader = new StreamReader(stream))
            {
                return await reader.ReadToEndAsync();
            }
}