#. Net crawls remote pages using get and post methods respectively.

Source: Internet
Author: User

 

Code

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. 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/xxxxxxxxxxxxxxxxxxxxxxxxxx ");
// 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 ));
}
Catch (Exception ex)
{
Err
= Ex. message;
Return   String . Empty;
}
}

 

 

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.