HttpWebRequest.BeginGetResponse

I need to make an asynchronous request for a web resource and use the example from this page ( link to the full example ):

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

But when I test the application, the execution freezes (for 2-3 seconds) in the last line of this code (I can see it using the debugger).

Why? Is this my mistake or is this the standard behavior of a function?

+6
source share
4 answers

You can try, I'm sure itโ€™s 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; } 

Due to chross thread of textbox'value, but this is a wpf application, I will return it, by the way, you can use webclient, for example

  private void tbWord_TextChanged(object sender, TextChangedEventArgs e) { WebClient wc = new WebClient(); wc.DownloadStringCompleted += HttpsCompleted; wc.DownloadStringAsync(new Uri("http://en.wikipedia.org/w/api.php?action=opensearch&search=" + tbWord.Text)); } private void HttpsCompleted(object sender, DownloadStringCompletedEventArgs e) { if (e.Error == null) { //do what ever //with using e.Result } } 
+6
source

The answer occurs in a separate thread. Winforms are not multi-threaded, so you have to send the call to the same thread as the form.

You can do this using the inner outline of the message in the window. Fortunately, .NET provides a way to do this. You can use the Invoke or BeginInvoke methods for this. The first blocks the current thread until the user interface thread completes the called method. Later does this asynchronously. If there is no cleaning, you can use the latter to โ€œshoot and forgetโ€

To do this, in any case, you need to create a method that BeginInvoke calls, and you need the delegate to point to this method.

For more information, see Control.Invoke and Control.BeginInvoke on MSDN.

Here is an example at this link: https://msdn.microsoft.com/en-us/library/zyzhdc6b(v=vs.110).aspx

Update: when I look at my profile because I forgot that I have an account here, I noticed this, and I have to add: everything that was in the past 3.5, or when they significantly changed the asynchronous stream model, here is from my wheelhouse. I am professional, and while I still love the craft, I do not follow every progress. I can tell you that this should work in all versions of .NET, but it may not be the absolute peak of performance 4.0 or higher, or in Mono / Winforms emulation if it is still around. On the bright side, any hit will usually not be bad outside of server applications, and even inside if threadpool does its job. Therefore, in most cases, do not focus on optimization efforts, and most likely they will work on the "stripped down" platforms that you see, for example, on C # mobile devices, although I had to be sure and most of them do not start winforms, but some spin message loops, and this also works. In fact, this is not the "best answer" for the latest platforms in each last case. But it can be more portable in the right case. If this helps one person avoid a design error, then it is worth the time to write it. =)

+2
source

You can use BackgroundWorker add to do it all in DoWork

+1
source

This is standard behavior.

From the documentation for the HttpWebRequest.BeginGetResponse Method :

The BeginGetResponse method requires some synchronous configuration tasks (for example, resolving DNS, detecting proxies, and connecting TCP sockets) before this method becomes asynchronous. [...] it may take a considerable time (up to several minutes, depending on network settings) to complete the initial tasks of synchronous setup before an exception for the error is selected or the method is successful.

In order not to wait for the installation, you can use the HttpWebRequest.BeginGetRequestStream Method but keep in mind that:

Your application cannot mix synchronous and asynchronous methods for a specific request. If you call the BeginGetRequestStream method, you must use the BeginGetResponse method to get a response.

+1
source

Source: https://habr.com/ru/post/908953/


All Articles