Windows Phone 7 (WP7) Development Network Operations (1) Basic GET requests for httpwebrequest

Source: Internet
Author: User

I. Prerequisites:

What is a GET request? What is URL? Direct Baidu, Google, and Bing.

2. WP7 network operations:Non-blocking asynchronous operations (direct synchronous operations are not yet seen ).

Iii. Main Code:

public class Http
{
public delegate void HandleResult(string result);
private HandleResult handle;

public void StartRequest(string Url, HandleResult handle)
{
this.handle = handle;
var webRequest = (HttpWebRequest)WebRequest.Create(Url);
webRequest.Method = "GET";
try
{
webRequest.BeginGetResponse(new AsyncCallback(HandleResponse), webRequest);
}
catch
{
}
}

public void HandleResponse(IAsyncResult asyncResult)
{
HttpWebRequest httpRequest = null;
HttpWebResponse httpResponse = null;
string result = string.Empty;
try
{
httpRequest = (HttpWebRequest)asyncResult.AsyncState;
httpResponse = (HttpWebResponse)httpRequest.EndGetResponse(asyncResult);

using (var reader = new StreamReader(httpResponse.GetResponseStream(), Encoding.UTF8))
{
result = reader.ReadToEnd();
reader.Close();
}
}
catch
{

}
finally
{
if (httpRequest != null) httpRequest.Abort();
if (httpResponse != null) httpResponse.Close();
}
handle(result);
}
}

 

Iv. Usage:

This is a simple get operation encapsulation class. You only need to call it as follows:

VaR HTTP = new HTTP ();
HTTP. startrequest (@ "http://www.baidu.com ",
Result =>
{
// Process the returned result
});

The first parameter of startrequest is the request URL. Of course, only Baidu's URL is written for convenience.

The second parameter is our processing proxy function for the result. The anonymous method is used for convenience.

V. Problems and analysis:

1. If you need to perform operations on the interface elements after obtaining the request results, do not forget to use deployment in handle. current. dispatcher. begininvoke (), or, a more direct method is to modify the last sentence of handleresponse to deployment. current. dispatcher. begininvoke () => handle (result ));

2. There is an extreme situation, that is, the network condition is poor, and the request must be sent long enough. Such a request will last for several seconds, assume that the network request is called by the Processing Event of a button pressed on the interface, the interface will be stuck. Here is an easy misunderstanding: I think that the WP7 network is asynchronous, so I can not use multithreading. Most of the time, this misunderstanding is not easy to find, mainly because the network is not bad, and the amount of data sent by GET requests is not large. However, the extreme situation is discussed, in order to provide a perfect user experience, it is worth a little effort on this issue. The Asynchronous Method of WP7 is only used to send the request and wait for the request asynchronously. The process of sending the request is still in the synchronous State. Therefore, the above startrequest method needs to be modified:

webRequest.BeginGetResponse(new AsyncCallback(HandleResponse), webRequest);

Replace:

new Thread(() =>webRequest.BeginGetResponse(new AsyncCallback(HandleResponse), webRequest)).Start();

(I have to say that the anonymous function of C # provides us with great convenience .)

Vi. Source Code:

Http://vdisk.weibo.com/s/3baOy

 

 

Reprinted please indicate the source:

Windows Phone 7 (WP7) Development Network Operations (1) Basic GET requests for httpwebrequest

Jin Yanyun

Http://www.cnblogs.com/vistach/archive/2012/03/14/Windows_Phone_WP7_Net_Http_HttpWebRequest_Get.html

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.