In Windows Phone, you can easily send network requests and obtain network data through the HttpWebRequest class. HttpWebRequest is an asynchronous operation that does not block the main thread.
1. You can create an HttpWebRequest using the HttpWebRequest. CreateHttp () method. The following code simply sends a GET request.
HttpGet
Public void httpGet () {try {// request address String url = "http://www.cnblogs.com/huizhang212/"; // create a WebRequest class HttpWebRequest request = HttpWebRequest. createHttp (new Uri (url); // sets the request method to get post request. method = "GET"; // return the Status request for responding to the asynchronous operation of the request. beginGetResponse (responseCallback, request);} catch (WebException e) {// network-related Exception handling} catch (Exception e) {// Exception Handling }}
2. Receive response data.
ResponseCallback
Private void responseCallback (IAsyncResult result) {try {// gets the information returned by the Asynchronous Operation HttpWebRequest request = (HttpWebRequest) result. asyncState; // end the asynchronous request for Internet resources HttpWebResponse response = (HttpWebResponse) request. endGetResponse (result); // parse the response Header // parseRecvHeader (response. headers); // obtain the length of the Request body long contentLength = response. contentLength; // get the response code int statusCode = (int) response. statusCode; string statusText = response. statusDescription; // The using (Stream stream = response. getResponseStream () {// GET request information StreamReader read = new StreamReader (stream); string msg = read. readToEnd (); Deployment. current. dispatcher. beginInvoke () =>{ textBlock1.Text = msg;}) ;}} catch (WebException e) {// Connection Failed} catch (Exception e) {// Exception Handling }}
HttpWebResponse can be used to obtain the returned data. After obtaining the data, you need to display the data on the interface. HttpWebRequest is an asynchronous operation, so there should be a thread to process the network. As we all know, in Windows Phone, the thread cannot operate the UI. This must be handled by a main UI thread, so Deployment is used in the code. current. dispatcher. beginInvoke.
3. The above is a simple GET request. the post request and get request have an additional operation process to send the request body. The following code is a POST request. Some operation functions are still responseCallback.
HttpPost
Public void httpPost () {try {// request address String url = "http://www.cnblogs.com/huizhang212/"; // create a WebRequest class HttpWebRequest request = HttpWebRequest. createHttp (new Uri (url); // sets the request method to get post request. method = "POST"; // return the status request for the asynchronous request. beginGetRequestStream (requestCallback, request);} catch (WebException e) {// network-related Exception handling} catch (Exception e) {// Exception Handling} private void requestCallback (IAsyncResult result) {try {// obtain the information returned by the Asynchronous Operation HttpWebRequest request = (HttpWebRequest) result. asyncState; // end the asynchronous request for Internet resources StreamWriter postStream = new StreamWriter (request. endGetRequestStream (result); postStream. writeLine ("Author: Yu zhile"); postStream. writeLine ("Source: http://www.cnblogs.com/huizhang212/"); // returns the status request for the asynchronous request. beginGetResponse (responseCallback, request);} catch (WebException e) {// Exception Handling} catch (Exception e) {// Exception Handling }}