Windows Phone network HttpWebRequest usage

Source: Internet
Author: User
Tags response code

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 }}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.