HttpWebRequest class for network programming of Windows Phone 8.1

Source: Internet
Author: User
Tags blank page

There are two classes in Windows phone that can implement network requests for HTTP protocols: HttpWebRequest class HttpClient class

The former is ideal for handling simple network requests, which support the HTTP requests more powerfully and are suitable for complex network request encapsulation.


But before you do, you need to know two ways to request http: Get requests and post requests.

The difference is: GET request: Get the data from the server, submit the data through the URI, the data can be seen in the URI, the data submitted at the same time only

can have 1024 bytes. POST request: Transfer data to the server, submit it by writing to the data stream, and POST request has no limit on the size of the submitted data

System.


In a network request, there must be a wide variety of indeterminate errors, which raise WebException, whose Status property contains an indication of the source of the error

WebExceptionStatus. The enumeration values are as follows:

Success: Success

ConnectFailure: Remote server Connection failed

Sendfailure: Send failed

RequestCanceled: The request was canceled

Pending: Internal asynchronous request hangs

Unknownerror: Unknown error

Messagelengthlimitexceeded: Limited message length for network requests


Example code:

Using system;using system.collections.generic;using system.io;using system.linq;using System.Net;using System.runtime.interopservices.windowsruntime;using system.text;using windows.foundation;using Windows.foundation.collections;using windows.ui.xaml;using windows.ui.xaml.controls;using Windows.ui.xaml.controls.primitives;using windows.ui.xaml.data;using windows.ui.xaml.input;using windows.ui.xaml.media;using windows.ui.xaml.navigation;//"Blank page" item template in http://go.microsoft.com/fwlink/?    linkid=390556 on the namespace app1{////<summary>//////for self or to navigate to a blank page inside the Frame.        </summary> public sealed partial class Httpwebrequestdemo:page {public Httpwebrequestdemo () {this.            InitializeComponent ();            WebRequest and HttpWebRequest HttpWebRequest request = httpwebrequest.createhttp ("http://www.baidu.com"); Set the requested method request.            Method = "Get"; Request.            Method = "Post";      Specifies the collection of name/value pairs that make up the HTTP header      Request.            headers["Cookie"] = "Stuname = value"; Sets the requested authentication information request.            Credentials = new NetworkCredential ("username", "password"); Initiates an GetResponse request to begin an asynchronous request for an Internet resource.            BeginGetResponse (responsecallback, request); Request.        BeginGetRequestStream (responsestreamcallback, request); }//Send response callback method for request to send data stream//private void Responsestreamcallback (IAsyncResult ar)//{//HTTPW Ebrequest HttpWebRequest = (HttpWebRequest) ar.        asyncstate; using (Stream stream = Httpwebrequest.endgetrequeststream (AR))//{//String content = "tes        Tstring ";        byte[] data = Encoding.UTF8.GetBytes (content); Stream. Write (data, 0, data.        Length);        }//Httpwebrequest.begingetresponse (Responsecallbackpost, HttpWebRequest);  }//private void Responsecallbackpost (IAsyncResult ar)//{////And Get request callback method like      }//Request fallback method private void Responsecallback (IAsyncResult ar) {HttpWebRequest httpweb Request = (HttpWebRequest) ar.            asyncstate;            WebResponse WebResponse = Httpwebrequest.endgetresponse (AR); Gets the content returned by the request using (Stream stream = WebResponse.GetResponseStream ()) using (StreamReader sr = new Strea Mreader (Stream)) {//Request the contents of the string returned by string content = Sr.            ReadToEnd ();        }}///<summary>///This page will be called when it is displayed in Frame.        </summary>//<param name= "E" > Describes how to access event data for this page. This parameter is typically used for configuration pages. </param> protected override void Onnavigatedto (NavigationEventArgs e) {}}}

HttpWebRequest class for network programming of Windows Phone 8.1

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.