C #. Net crawls remote pages by get and Post respectively

Source: Internet
Author: User

Introduce namespace
Using system. IO;
Using system. net;
Using system. text;
Using system. Text. regularexpressions;

// Capture remote page content in get Mode
Public String get_http (string Turl)
{
String strresult;
Try
{
Httpwebrequest HWR = (httpwebrequest) httpwebrequest. Create (Turl );
HWR. Timeout = 19600;
Httpwebresponse hwrs = (httpwebresponse) HWR. getresponse ();
Stream mystream = hwrs. getresponsestream ();
Streamreader sr = new streamreader (mystream, encoding. Default );
Stringbuilder sb = new stringbuilder ();
While (-1! = Sr. Peek ())
{
SB. append (Sr. Readline () + "\ r \ n ");
}
Strresult = sb. tostring ();
Hwrs. Close ();
}
Catch (exception ee)
{
Strresult = ee. message;
}
Return strresult;
}

// Capture remote page content in post Mode
// Postdata is the parameter list
Public String post_http (string URL, string postdata, string encodetype, out string ERR)
{
String uristring = URL;
Byte [] bytearray;
Byte [] responsearray;
// Postdata = "checkvalue = 32 & BBB = % Cd % B6 % C6 % B1 & ILC = 0 & KKK = 22 ";
Encoding encoding = encoding. getencoding (encodetype );
Try
{
WebClient mywebclient = new WebClient ();
Webheadercollection mywebheadercollection;
Mywebclient. headers. Add ("Content-Type", "application/X-WWW-form-urlencoded ");
// Mywebclient. headers. Add ("Referer", "http: // XXXXX/xxxxxxxxxxxxxxxxxxxxxxxx ");
// Mywebclient. headers. Add ("Accept-language", "ZH-CN ");
Mywebheadercollection = mywebclient. headers;

bytearray = encoding. getbytes (postdata);
responsearray = mywebclient. uploaddata (uristring, "Post", bytearray);
err = string. empty;
return this. strclearnouse (encoding. getstring (responsearray);
}< br> catch (exception ex)
{< br> err = ex. message;
return string. empty;
}< BR >}

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.