Two methods to obtain the content of a remote webpage

Source: Internet
Author: User

 

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;
}

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.