Request.BeginGetResponse probem in Windows 7 and unexpected Blocking behaviour
-
Hello Readers, I am downloading a Html File using the following method.
public void LoadInitialData()
{
HttpWebRequest request = null;try { Stopwatch sw = new Stopwatch(); RaiseStateChangedEvent(10, "Creating Connection to Server"); request = (HttpWebRequest)(WebRequest.Create(this.\_ServerUrl)); RaiseStateChangedEvent(20, "Connected to Server"); int timeout = request.Timeout; if (request != null) { try { sw.Start(); request.BeginGetResponse(HttpRequestCallBackMethod, request); sw.Stop(); RaiseStateChangedEvent(40, "Waiting for Data from Server --Time Taken: " + sw.ElapsedMilliseconds); } catch (Exception ex) { if (onErrorOccured != null) onErrorOccured(ex.Message + "\\n" + ex.StackTrace); } } } catch (Exception ex) { if (onErrorOccured != null) onErrorOccured(ex.Message + "\\n" + ex.StackTrace); } } private void HttpRequestCallBackMethod(IAsyncResult iar) { RaiseStateChangedEvent(70, "Data Received From Server"); HttpWebRequest myHttpWebRequest = iar.AsyncState as HttpWebRequest; HttpWebResponse response = (HttpWebResponse)myHttpWebRequest.EndGetResponse(iar); using (StreamReader input = new StreamReader(response.GetResponseStream())) { string data = input.ReadToEnd(); RaiseStateChangedEvent(80, "Processing received Data"); ProcessData(data); RaiseStateChangedEvent(100, "Completed Loading Initial Data"); } } private void RaiseStateChangedEvent(int progress, string message) { if (onStateChanged != null) onStateChanged.Invoke(progress, message); } private void ProcessData(string data) { Console.WriteLine("Download Completed - Do any Processing here"); }
The program is written in .Net 2.0. When I try to run the same in Windows 7, I am not successful. I
-
Hello Readers, I am downloading a Html File using the following method.
public void LoadInitialData()
{
HttpWebRequest request = null;try { Stopwatch sw = new Stopwatch(); RaiseStateChangedEvent(10, "Creating Connection to Server"); request = (HttpWebRequest)(WebRequest.Create(this.\_ServerUrl)); RaiseStateChangedEvent(20, "Connected to Server"); int timeout = request.Timeout; if (request != null) { try { sw.Start(); request.BeginGetResponse(HttpRequestCallBackMethod, request); sw.Stop(); RaiseStateChangedEvent(40, "Waiting for Data from Server --Time Taken: " + sw.ElapsedMilliseconds); } catch (Exception ex) { if (onErrorOccured != null) onErrorOccured(ex.Message + "\\n" + ex.StackTrace); } } } catch (Exception ex) { if (onErrorOccured != null) onErrorOccured(ex.Message + "\\n" + ex.StackTrace); } } private void HttpRequestCallBackMethod(IAsyncResult iar) { RaiseStateChangedEvent(70, "Data Received From Server"); HttpWebRequest myHttpWebRequest = iar.AsyncState as HttpWebRequest; HttpWebResponse response = (HttpWebResponse)myHttpWebRequest.EndGetResponse(iar); using (StreamReader input = new StreamReader(response.GetResponseStream())) { string data = input.ReadToEnd(); RaiseStateChangedEvent(80, "Processing received Data"); ProcessData(data); RaiseStateChangedEvent(100, "Completed Loading Initial Data"); } } private void RaiseStateChangedEvent(int progress, string message) { if (onStateChanged != null) onStateChanged.Invoke(progress, message); } private void ProcessData(string data) { Console.WriteLine("Download Completed - Do any Processing here"); }
The program is written in .Net 2.0. When I try to run the same in Windows 7, I am not successful. I
Hello Readers, I found the solution for the problem I mentioned. Just add to lines of Code to modify the properties of request object. Delay in the in the initial call is reduced by setting the Proxy as null. This happened in all versions of windows.
request.Proxy = null;
Also, In Windows 7, If I send 10 requests, I am only getting 5 or 6 responses. By setting KeepAlive property to false, I am getting all responses.
request.KeepAlive = true;
Hope this helps someone! Happy Coding...