在WP7中的HttpWebRequest与在C#中的使用方式大致是一样的,但是在WP7中微软移去了同步的操作方式,所有的操作方式都改为异步,(提升用户体验,在请求过程中界面保持流畅)。
下面给出两种编码方式 :其实都是一样的,只是第一种使用了独立方法的完成,(便于阅读和理解),第二种是直接用委托写在同一个方法里。
第一种:
1 CookieContainer cc = null; 2 private void button1_Click(object sender, RoutedEventArgs e) 3 { 4 string url = "http://www.cnblogs.com/xdpxyxy"; 5 6 HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(new Uri(url)); 7 8 if (cc == null) 9 { 10 cc = new CookieContainer(); 11 } 12 myRequest.CookieContainer = cc; 13 14 myRequest.Method = "POST"; 15 16 myRequest.ContentType = "application/x-www-form-urlencoded"; 17 18 myRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), myRequest); 19 20 } 21 22 /// <summary> 23 /// 写入Stream的异步请求 24 /// </summary> 25 /// <param name="asynchronousResult"></param> 26 private void GetRequestStreamCallback(IAsyncResult asynchronousResult) 27 { 28 29 HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState; 30 31 System.IO.Stream postStream = request.EndGetRequestStream(asynchronousResult); 32 33 //POST提交服务器的资源 34 string PostData = ""; 35 36 byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(PostData); 37 38 postStream.Write(byteArray, 0, PostData.Length); 39 40 postStream.Close(); 41 42 request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request); 43 44 } 45 46 /// <summary> 47 /// 对服务器资源异步请求 48 /// </summary> 49 /// <param name="asynchronousResult"></param> 50 private void GetResponseCallback(IAsyncResult asynchronousResult) 51 { 52 53 HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState; 54 55 HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult); 56 57 Stream streamResponse = response.GetResponseStream(); 58 59 StreamReader streamRead = new StreamReader(streamResponse); 60 61 string responseString = streamRead.ReadToEnd(); 62 63 streamRead.Close(); 64 65 streamResponse.Close(); 66 67 }
第二种 :
1 HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(new Uri(PostUrl)); 2 httpWebRequest.ContentType = "application/x-www-form-urlencoded"; 3 httpWebRequest.Method = "POST"; 4 if (co == null) 5 { 6 httpWebRequest.CookieContainer = new CookieContainer(); 7 } 8 else 9 { 10 httpWebRequest.CookieContainer = co; 11 } 12 httpWebRequest.BeginGetRequestStream((IAsyncResult streamCallback) => 13 { 14 HttpWebRequest webRequest = streamCallback.AsyncState as HttpWebRequest; 15 16 byte[] buffer = Encoding.UTF8.GetBytes(PostData); 17 Stream stream = webRequest.EndGetRequestStream(streamCallback); 18 stream.Position = 0; 19 stream.Write(buffer, 0, buffer.Length); 20 stream.Close(); 21 webRequest.BeginGetResponse((IAsyncResult responseCallback) => 22 { 23 HttpWebRequest webRequest2 = responseCallback.AsyncState as HttpWebRequest; 24 HttpWebResponse webResponse = (HttpWebResponse)webRequest2.EndGetResponse(responseCallback); 25 Stream streamResponse = webResponse.GetResponseStream(); 26 StreamReader sr = new StreamReader(streamResponse); 27 string str = sr.ReadToEnd(); 28 }, webRequest); 29 }, httpWebRequest);