I need cookies to retrieve pages. If I do not need them, I can remove them;
GET method:
Copy codeThe Code is as follows:
/// <Summary>
/// Obtain the HTML content accessed by the URL
/// </Summary>
/// <Param name = "Url"> URL </param>
/// <Returns> HTML content </returns>
Public static string GetWebContent (string Url, CookieContainer cookieContainer)
{
String strResult = "";
Try
{
ServicePointManager. Expect100Continue = true;
ServicePointManager. SecurityProtocol = SecurityProtocolType. Ssl3;
HttpWebRequest request = (HttpWebRequest) WebRequest. Create (Url );
Request. CookieContainer = cookieContainer;
Request. Timeout = 30000;
Request. Headers. Set ("Pragma", "no-cache ");
HttpWebResponse response = (HttpWebResponse) request. GetResponse ();
Stream streamReceive = response. GetResponseStream ();
Encoding encoding = Encoding. GetEncoding ("UTF-8 ");
StreamReader streamReader = new StreamReader (streamReceive, encoding );
StrResult = streamReader. ReadToEnd ();
}
Catch
{
}
Return strResult;
}
POST method:
Copy codeThe Code is as follows:
/// <Summary>
/// Post submit data to https
/// </Summary>
/// <Param name = "posturl"> </param>
/// <Param name = "postdata"> </param>
/// <Param name = "header"> </param>
/// <Param name = "cookieContainer"> </param>
/// <Returns> </returns>
Public static string SetPostHtml (string posturl, string postdata, HttpHeader header, CookieContainer cookieContainer)
{
String restr = "";
ServicePointManager. Expect100Continue = true;
ServicePointManager. SecurityProtocol = SecurityProtocolType. Ssl3;
HttpWebRequest request = null;
HttpWebResponse response = null;
Request = (HttpWebRequest) WebRequest. Create (posturl );
Request. CookieContainer = cookieContainer;
Request. Method = header. method;
Request. Referer = header. Referer;
Request. ContentType = header. contentType;
Byte [] postdatabyte = Encoding. UTF8.GetBytes (postdata );
Request. ContentLength = postdatabyte. Length;
Request. AllowAutoRedirect = false;
Request. KeepAlive = true;
// Submit the request
Stream stream;
Stream = request. GetRequestStream ();
Stream. Write (postdatabyte, 0, postdatabyte. Length );
Stream. Close ();
// Receive response
Response = (HttpWebResponse) request. GetResponse ();
Using (StreamReader reader = new StreamReader (response. GetResponseStream ()))
{
Restr = reader. ReadToEnd (). ToString ();
}
Return restr;
}