Introduction to the usage of Windows phone network HttpWebRequest

Source: Internet
Author: User
Tags exception handling

In Windows Phone system, the HttpWebRequest class makes it easy to send network requests and get network data. HttpWebRequest is an asynchronous operation and does not block the main thread.

1. The Httpwebrequest.createhttp () method allows you to create a HttpWebRequest that simply implements sending a GET request.

HttpGet public
    
        void HttpGet ()
        {
            try
            {
                //request address
                String URL = ' http://www.cnblogs.com/ huizhang212/";
                Create WebRequest class
                HttpWebRequest request = httpwebrequest.createhttp (new Uri (URL));
    
                Sets the request method get POST request
                . method = ' Get ';
    
                Returns the status request for an answer requesting an asynchronous operation
                . BeginGetResponse (responsecallback, request);
            }
            catch (WebException e)
            {
                //network-related exception handling
            }
            catch (Exception e)
            {
                //exception handling
            }
        }

2. Answer the data receiving section.

Responsecallback private void Responsecallback (IAsyncResult result) {try { Gets the information returned by the asynchronous operation HttpWebRequest request = (HttpWebRequest) result.
                asyncstate; Ends an asynchronous request for an Internet resource httpwebresponse response = (HttpWebResponse) request.
                EndGetResponse (result); Resolves the answer Head//parserecvheader (response.
                Headers); Gets the request body information length long contentlength = Response.
    
                ContentLength; Gets the answer code int statusCode = (int) Response.
                StatusCode; String statustext = Response.
    
                Statusdescription; The answer header information verifies the using (Stream stream = response. GetResponseStream ()) {//GET request information StreamReader Read = new Streamread
                    ER (stream); String msg = read.
                    ReadToEnd (); Deployment.Current.Dispatcher.BeginInvOke (() => {textblock1.text = msg;
                });
                   
            The catch (WebException e) {//Connection failed
    
        catch (Exception e) {//exception handling} }

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/OS/extra/

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.