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/