HttpRequest Get and Post call methods for other pages

Source: Internet
Author: User

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;
}

Related Article

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.