demas demas -4 years ago 221
C# Question


I need to make async request to web resource and use example from this page (link to full example):

HttpWebRequest myHttpWebRequest= (HttpWebRequest)WebRequest.Create("");
RequestState myRequestState = new RequestState();
myRequestState.request = myHttpWebRequest;
// Start the asynchronous request.
IAsyncResult result=
(IAsyncResult) myHttpWebRequest.BeginGetResponse(new AsyncCallback(RespCallback),myRequestState);

But when I am testing the application the execution freeze(on 2-3 sec) on the last line of this code (i can watch it using debugger).

Why? Is it my mistake or it is a standard behaviour of the function?

Answer Source

You can try, I m sure thats better

private void StartWebRequest(string url)
   HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
   request.BeginGetResponse(new AsyncCallback(FinishWebRequest), request);

private void FinishWebRequest(IAsyncResult result)
   HttpWebResponse response = (result.AsyncState as HttpWebRequest).EndGetResponse(result) as HttpWebResponse;

Because of chross thread of textbox'value,But this is wpf application i will retag this, btw you can use webclient like

 private void tbWord_TextChanged(object sender, TextChangedEventArgs e)
        WebClient wc = new WebClient();
        wc.DownloadStringCompleted += HttpsCompleted;
        wc.DownloadStringAsync(new Uri("" + tbWord.Text)); 
    private void HttpsCompleted(object sender, DownloadStringCompletedEventArgs e)
        if (e.Error == null)

             //do what ever 
             //with using e.Result
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download