Method 1: webrequest
Private string getstringbyurl (string strurl)
{
Webrequest WRT = webrequest. Create (strurl );
Webresponse wrse = WRT. getresponse ();
Stream STRM = wrse. getresponsestream ();
Streamreader sr = new streamreader (STRM, encoding. getencoding ("gb2312 "));
String strallstrm = Sr. readtoend ();
Return strallstrm;
}
Method 2: httpwebrequest
Public static string getpage (string URL, encoding)
{
Httpwebrequest request = NULL;
Httpwebresponse response = NULL;
Streamreader reader = NULL;
Try
{
Request = (httpwebrequest) webrequest. Create (URL );
Request. useragent = "www.svnhost.cn ";
Request. Timeout = 20000;
Request. allowautoredirect = false;
Response = (httpwebresponse) request. getresponse ();
If (response. statuscode = httpstatuscode. OK & response. contentlength <1024*1024)
{
Reader = new streamreader (response. getresponsestream (), encoding );
String html = reader. readtoend ();
Return HTML;
}
}
Catch
{
}
Finally
{
If (response! = NULL)
{
Response. Close ();
Response = NULL;
}
If (reader! = NULL)
Reader. Close ();
If (request! = NULL)
Request = NULL;
}
Return string. empty;
}
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/yefengzhixia/archive/2009/07/01/4312127.aspx