Window phone WebClient and httpwebrequest and solve the gb2312 garbled Problem

Source: Internet
Author: User

1. WebClient class

In the system. Net Space, classes are provided to send data to the target of the URI identifier and receive data from the target of the URI identifier.

Openreadcompleted

Private void dowebclient ()
{
String url = "http://www.qq.com ";
WebClient client = new WebClient ();
Client. openreadasync (New uri (URL ));
Client. openreadcompleted + = new openreadcompletedeventhandler (webclient_openreadcompleted );
}
Private void webclient_openreadcompleted (Object sender, openreadcompletedeventargs E)
{
Using (streamreader reader = new streamreader (E. Result, dbcscodepage. dbcsencoding. getdbcsencoding ("gb2312 ")))
{
String contents = reader. readtoend ();
Int begin = contents. tostring (). indexof ("<title> ");
Int end = contents. tostring (). indexof ("</title> ");
// UI thread call to directly update the interface
Clienttext. Text = contents. tostring (). substring (begin + 7, end-begin-7 );
}
}

2. httpwebrequest class

Provides the http implementation of the webrequest class in the system. Net Space.

Private void dohttpwebrequest ()
{
String url = "http://www.baidu.com ";
Webrequest request = httpwebrequest. Create (URL );
Iasyncresult result = request. begingetresponse (responsecallback, request );
}

Private void responsecallback (iasyncresult result)
{

Httpwebrequest Req = result. asyncstate as httpwebrequest;
Webresponse res = Req. endgetresponse (result );
Using (Stream stream = res. getresponsestream ())
Using (streamreader reader = new streamreader (stream, dbcscodepage. dbcsencoding. getdbcsencoding ("gb2312 ")))
{
String contents = reader. readtoend ();
Int begin = contents. tostring (). indexof ("<title> ");
Int end = contents. tostring (). indexof ("</title> ");
// Runs based on the background, not in the UI thread
Dispatcher. begininvoke () =>{ reqtext. Text = contents. tostring (). substring (begin + 7, end-begin-7 );});
}

}

PS: dbcscodepage is used to solve the garbled problem.

Source code: http://download.csdn.net/detail/wulongtiantang/5072852

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.