CopyCode The Code is as follows: // GET Request Method
Private string requestget (string URL)
{
String pagestr = string. Empty; // html for storing and returning
Uri url = new uri (URL); // The URI class provides the Object Representation of the Uniform Resource Identifier (URI) and easy access to each part of the URI. Is to process the URL address
Try
{
Httpwebrequest httprequest = (httpwebrequest) webrequest. Create (URL); // create an httpwebrequest object based on the URL
# Region parameter settings
Httprequest. method = "get ";
// ----------------------------------------------- Set some parameters (optional)
// Httprequest. keepalive = false; // set persistent connection to false
// Httprequest. protocolversion = httpversion. version11; // Network Protocol version
// Httprequest. Proxy = WebProxy. getdefaproxy proxy (); // server proxy
// Httprequest. contenttype = "application/X-WWW-form-urlencoded"; // HTTP Header
// Httprequest. allowautoredirect = true;
// Httprequest. maximumautomaticredirections = 10;
// Httprequest. Timeout = 30000; // set timeout to 10 seconds (milliseconds)
// Httprequest. useragent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"; // Browser
// ================================================ ==============
# Endregion
Httpwebresponse response = (httpwebresponse) httprequest. getresponse (); // use httpwebresponse to obtain the return value of the request
Stream steam = response. getresponsestream (); // obtain the data stream from the returned object
Streamreader reader = new streamreader (steam, encoding. getencoding ("gb2312"); // read data encoding. getencoding ("gb2312") indicates that the encoding is gb2312, so that Chinese characters are not garbled.
Pagestr = reader. readtoend ();
Reader. Close ();
}
Catch (exception E)
{
Pagestr + = E. message;
}
Return pagestr;
}
Copy code The Code is as follows: // the POST request method is similar to the get method, so the explanation is a little less.
Private string requestpost (string URL, string context) // the two parameters are the URL address and post data respectively.
{
String pagestr = string. empty;
Uri url = new uri (URL );
Byte [] reqbytes = encoding. ASCII. getbytes (context );
Try
{
Httpwebrequest Req = (httpwebrequest) webrequest. Create (URL );
Req. method = "Post ";
Req. contenttype = "application/X-WWW-form-urlencoded ";
Req. contentlength = reqbytes. length;
Stream STM = Req. getrequeststream ();
STM. Write (reqbytes, 0, reqbytes. Length );
STM. Close ();
Httpwebresponse wR = (httpwebresponse) Req. getresponse ();
Stream stream = Wr. getresponsestream ();
Streamreader SRD = new streamreader (stream, encoding. getencoding ("gb2312 "));
Pagestr + = SRD. readtoend ();
Stream. Close ();
SRD. Close ();
}
Catch (exception E)
{
Pagestr + = E. message;
}
Return pagestr;
}
Copy code The Code is as follows: Public String webclientget (string URL)
{
VaR client = new WebClient ();
Client. headers. Add ("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2;. Net CLR 1.0.3705 ;)");
VaR stream = client. openread (URL );
If (Stream = NULL) Return "";
VaR reader = new streamreader (stream, encoding. Default );
VaR result = reader. readtoend ();
Stream. Close ();
Reader. Close ();
Return result;
}