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