1

I am trying to make asynchronous call to some server so that the main thread does not stop for the request.

I created my request something like this :

//Create Request
 HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
 request.Method = method;
 request.ContentType = "application/XML";
 System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
 byte[] bytes = encoding.GetBytes(data);
 request.ContentLength = bytes.Length;
 try
 {
 using (Stream requestStream = request.GetRequestStream())
 {
 // Send the data.
 requestStream.Write(bytes, 0, bytes.Length);
 }
 request.BeginGetResponse((x) =>
 {
 using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(x))
 {
 if (callback != null)
 {
 callback(response.GetResponseStream());
 }
 }
 }, null);
 }
 catch (Exception ex)
 {
 //TODO: Log error
 }

But still the thread stops for some time in this request. Especially on the line using

(Stream requestStream = request.GetRequestStream())

Is there any way by which we can make it completely asynchronous or some other better way so that our main thread does not hangs for this request.

asked Sep 30, 2014 at 16:14
3
  • Sure - call GetRequestStreamAsync and await the result. Commented Sep 30, 2014 at 16:17
  • 1
    Or just use HttpClient and await if you can - it's much more concise. Commented Sep 30, 2014 at 16:19
  • The documentation says that if you call GetRequestStream then you must call GetResponse to get the response. If you want an asynchronous response using BeginGetResonse, then you must call BeginGetRequeststream to get the request stream. Commented Sep 30, 2014 at 19:05

3 Answers 3

1
using (HttpClient client = new HttpClient())
{
 var content = new StringContent(data, Encoding.UTF8, "application/xml");
 var response = await client.PostAsync(uri, content);
 var respString = await response.Content.ReadAsStringAsync();
}
answered Sep 30, 2014 at 16:46
2
  • I am little confused here. In the request we actually wait for the response by putting await. Also if method A() makes the request then we have to call by await task A() and then wait for the response. Making it synchronous only. Commented Oct 1, 2014 at 6:13
  • @user2692032 What Happens in an Async Method Commented Oct 1, 2014 at 7:16
0

Need to use the async methods and await them.

using (var stream = await request.GetRequestStreamAsync())
{
 await stream.WriteAsync(bytes, 0, bytes.Length);
}
answered Sep 30, 2014 at 16:24
0

Here is a simple sample which I am using to make async request. Mark your method with async keyword

using (var httpClient = new HttpClient())
{
 string requestContent = string.Empty; // JSON Content comes here
 HttpContent httpContent = new StringContent(requestContent, Encoding.UTF8, "application/json");
 HttpRequestMessage httpRequestMessage = new HttpRequestMessage(Verb, url);
 httpRequestMessage.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
 httpRequestMessage.Content = request.Verb == HttpMethod.Get ? null : httpContent;
 response = httpClient.SendAsync(httpRequestMessage);
 var responseContent = await response.Result.Content.ReadAsStringAsync();

}

answered Sep 30, 2014 at 16:25

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.