Copy codeThe 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 codeThe Code is as follows:
// The Post request method is similar to the Get method, so there is less explanation.
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 codeThe 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;
}