c# – 实现HttpWebRequest异步调用

前端之家收集整理的这篇文章主要介绍了c# – 实现HttpWebRequest异步调用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有这个代码
  1. using (var stream = new StreamWriter(request.GetRequestStream(),Encoding))
  2. stream.Write(body.ToString());

我需要让它异步.据我所知,这意味着我需要将对request.GetRequestStream()的调用更改为asychronous.BeginGetRequestStream().我已经看到了this示例,但无法弄清楚如何将其应用于此场景.有人可以帮忙吗?

解决方法

文档有一个很好的例子( http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.begingetrequeststream(v=vs.100).aspx):
  1. using System;
  2. using System.Net;
  3. using System.IO;
  4. using System.Text;
  5. using System.Threading;
  6.  
  7. class HttpWebRequestBeginGetRequest
  8. {
  9. private static ManualResetEvent allDone = new ManualResetEvent(false);
  10. public static void Main(string[] args)
  11. {
  12. // Create a new HttpWebRequest object.
  13. HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.contoso.com/example.aspx");
  14.  
  15. request.ContentType = "application/x-www-form-urlencoded";
  16.  
  17. // Set the Method property to 'POST' to post data to the URI.
  18. request.Method = "POST";
  19.  
  20. // start the asynchronous operation
  21. request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback),request);
  22.  
  23. // Keep the main thread from continuing while the asynchronous
  24. // operation completes. A real world application
  25. // could do something useful such as updating its user interface.
  26. allDone.WaitOne();
  27. }
  28.  
  29. private static void GetRequestStreamCallback(IAsyncResult asynchronousResult)
  30. {
  31. HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
  32.  
  33. // End the operation
  34. Stream postStream = request.EndGetRequestStream(asynchronousResult);
  35.  
  36. Console.WriteLine("Please enter the input data to be posted:");
  37. string postData = Console.ReadLine();
  38.  
  39. // Convert the string into a byte array.
  40. byte[] byteArray = Encoding.UTF8.GetBytes(postData);
  41.  
  42. // Write to the request stream.
  43. postStream.Write(byteArray,postData.Length);
  44. postStream.Close();
  45.  
  46. // Start the asynchronous operation to get the response
  47. request.BeginGetResponse(new AsyncCallback(GetResponseCallback),request);
  48. }
  49.  
  50. private static void GetResponseCallback(IAsyncResult asynchronousResult)
  51. {
  52. HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
  53.  
  54. // End the operation
  55. HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
  56. Stream streamResponse = response.GetResponseStream();
  57. StreamReader streamRead = new StreamReader(streamResponse);
  58. string responseString = streamRead.ReadToEnd();
  59. Console.WriteLine(responseString);
  60. // Close the stream object
  61. streamResponse.Close();
  62. streamRead.Close();
  63.  
  64. // Release the HttpWebResponse
  65. response.Close();
  66. allDone.Set();
  67. }

猜你在找的C#相关文章