Method 1: Suitable for obtaining a small amount of data:
When using the clear test, it is found that only a portion of the webpage content is obtained, and the content at the bottom of the page is not displayed completely.
Code
/// <Summary>
/// Obtain the remote server result, suitable for obtaining a small amount of content
/// </Summary>
/// <Param name = "a_strurl"> remote URL </param>
/// <Param name = "timeout"> timeout, in milliseconds </param>
/// <Param name = "newline"> whether to wrap the result </param>
/// <Returns> </returns>
Public static string gethttp (string a_strurl, int timeout, bool newline)
{
String strresult = "";
Timeout = timeout = 0? 1000: timeout;
Try
{
Httpwebrequest myreq = (httpwebrequest) httpwebrequest. Create (a_strurl );
Myreq. Timeout = timeout;
Httpwebresponse httpwresp = (httpwebresponse) myreq. getresponse ();
Stream mystream = httpwresp. getresponsestream ();
Streamreader sr = new streamreader (mystream, encoding. Default );
Stringbuilder strbuilder = new stringbuilder ();
While (-1! = Sr. Peek ())
{
Strbuilder. append (Sr. Readline ());
If (newline)
Strbuilder. append ("\ r \ n ");
}
Strresult = strbuilder. tostring ();
Myreq = NULL;
Httpwresp. Close ();
Mystream. Close ();
Mystream. Close ();
Sr. Close ();
Strbuilder = NULL;
}
Catch (exception exp)
{
# If debug
Strresult = "gethttp error:" + exp. message;
# Else
Strresult = "time out ";
# Endif
}
Return strresult;
}
Method 2,Suitable for retrieving large amounts of data:
Code
/// <Summary>
/// Obtain remote URL content, suitable for obtaining a large amount of content
/// </Summary>
/// <Param name = "a_strurl"> remote URL </param>
/// <Param name = "encodetype"> encoding method of the target URL </param>
/// <Returns> </returns>
Public static string gethttp (string a_strurl, string encodetype)
{
String result = "";
If (encodetype. isnull ())
Encodetype = "gb2312 ";
WebClient mywebclient = new WebClient ();
Mywebclient. Credentials = credentialcache. defaultcredentials; // gets or sets the network creden。 used to authenticate requests to Internet resources.
Byte [] pagedata = mywebclient. downloaddata (a_strurl );
Result = encoding. getencoding (encodetype). getstring (pagedata );
Return result;
}