Wait until Web Request is completed in C# .Net

Last Reply on Jan 08, 2016 07:19 AM By Shashikant

Posted on Jan 08, 2016 07:16 AM

 

//Comparison of domains code start                     
                    WebRequest webRequest = WebRequest.Create(ytLink);
                    webRequest.Timeout = 10000;
                    //webRequest.r
                    webRequest.Credentials = CredentialCache.DefaultCredentials;
                    WebResponse webResponse = webRequest.GetResponse();
                    Console.WriteLine(((HttpWebResponse)webResponse).StatusDescription);
                    Stream dataStream = webResponse.GetResponseStream();                    
                    StreamReader reader = new StreamReader(dataStream);                    
                    string responseFromServer = reader.ReadToEnd(); 
                    reader.Close();
                    webResponse.Close();

how to wait till my request is completed and get the response

Babu

Posted on Jan 08, 2016 07:19 AM

Hi,

Please set webRequest.Timeout to 0,so it will wait till request is completed.

C#

WebRequest webRequest = WebRequest.Create(ytLink);
webRequest.Timeout = 0;
//webRequest.r
webRequest.Credentials = CredentialCache.DefaultCredentials;
WebResponse webResponse = webRequest.GetResponse();
Console.WriteLine(((HttpWebResponse)webResponse).StatusDescription);
Stream dataStream = webResponse.GetResponseStream();                   
StreamReader reader = new StreamReader(dataStream);                   
string responseFromServer = reader.ReadToEnd();
reader.Close();
webResponse.Close();

Note:

The GetResponse() method is a synchronous method that will only return after the server sends a reply.

The BeginGetResponse() method is an asynchronous method that will return immediately, before the server replies.