Obtain webpage HTML code

Source: Internet
Author: User
Public bool getweb (string strurl, out string BUF)
{
Buf = "";
Try
{
// URI url = new uri (strurl, false );
Httpwebrequest request;
Request = (httpwebrequest) webrequest. Create (strurl );
Request. method = "Post"; // POST Request Method
Request. contenttype = "text/html; charset = gb2312"; // content type
String paraurlcoded = system. Web. httputility. urlencode (""); // The parameter is URL encoded.
Byte [] payload;
Payload = system. Text. encoding. getencoding ("gb2312"). getbytes (paraurlcoded); // convert the URL-encoded string into bytes.
Request. contentlength = payload. length; // you can specify the request contentlength.
Stream writer = request. getrequeststream (); // get the request stream
Writer. Write (payload, 0, payload. Length); // write request parameters to the stream
Writer. Close (); // close the request stream
Httpwebresponse response;
Response = (httpwebresponse) request. getresponse (); // get the response stream
Stream S;
S = response. getresponsestream ();
Streamreader objreader = new streamreader (S, system. Text. encoding. getencoding ("gb2312 "));
String html = "";
String Sline = "";
Int I = 0;
While (Sline! = NULL)
{
I ++;
Sline = objreader. Readline ();
If (Sline! = NULL)
HTML + = Sline;
}
// Html = html. Replace ("& lt;", "<");
// Html = html. Replace ("& gt;", "> ");
Buf = HTML;
Return true;
}
Catch (exception X)
{
Buf = x. Message. tostring ();
Return false;
}
} With Cookie: cookiecontainer cc = new cookiecontainer (); Public bool getweb (string strurl, out string BUF)
{
Buf = "";
Try
{Httpwebrequest request;
Request = (httpwebrequest) webrequest. Create (strurl );
Request. method = "Post"; // POST Request Method
Request. contenttype = "text/html; charset = gb2312"; // content type
String paraurlcoded = system. Web. httputility. urlencode (""); // The parameter is URL encoded.
Byte [] payload;
Payload = system. Text. encoding. getencoding ("gb2312"). getbytes (paraurlcoded); // convert the URL-encoded string into bytes.
Request. contentlength = payload. length; // you can specify the request contentlength.
Stream writer = request. getrequeststream (); // get the request stream
Writer. Write (payload, 0, payload. Length); // write request parameters to the stream
Writer. Close (); // close the request stream
Httpwebresponse response;
Response = (httpwebresponse) request. getresponse (); // get the response stream
Stream S;
S = response. getresponsestream ();
Streamreader objreader = new streamreader (S, system. Text. encoding. getencoding ("gb2312 "));
String html = "";
String Sline = "";
Int I = 0;
While (Sline! = NULL)
{
I ++;
Sline = objreader. Readline ();
If (Sline! = NULL)
HTML + = Sline;
}

Buf = HTML;
Return true;
}
Catch (exception X)
{
Buf = x. Message. tostring ();
Return false;
}
}
Public bool getweb (string strurl, out string Buf, string postdata)
{
Buf = "";
Try
{

Asciiencoding encoding = new asciiencoding ();
Byte [] DATA = encoding. getbytes (postdata); httpwebrequest request = (httpwebrequest) webrequest. Create (strurl );
Request. method = "Post ";
Request. contenttype = "application/X-WWW-form-urlencoded ";
Request. contentlength = data. length;
Stream newstream = request. getrequeststream ();
Newstream. Write (data, 0, Data. Length );

Newstream. Close ();

Request. cookiecontainer = cc;

Httpwebresponse response = (httpwebresponse) request. getresponse ();
Cc. Add (response. Cookies );
Stream stream = response. getresponsestream ();
String shtml = new streamreader (stream, system. Text. encoding. Default). readtoend ();
Buf = shtml;
Return true;
}
Catch (exception X)
{
Buf = x. Message. tostring ();
Return false;
}
}

From http://www.cnblogs.com/jacktu/archive/2007/07/30/836365.html

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.