// Obtain the Cookie value after the current webBrowser Logon
[DllImport ("wininet. dll", CharSet = CharSet. Auto, SetLastError = true)]
Static extern bool InternetGetCookieEx (string pchURL, string pchCookieName, StringBuilder pchCookieData, ref int pcchCookieData, int dwFlags, object lpReserved );
// Retrieve the Cookie.
Private static string GetCookieString (string url)
{
// Determine the size of the cookie
Int datasize = 256;
StringBuilder cookieData = new StringBuilder (datasize );
If (! InternetGetCookieEx (url, null, cookieData, ref datasize, 0x00002000, null ))
{
If (datasize <0)
Return null;
// Allocate stringbuilder large enough to hold the cookie
CookieData = new StringBuilder (datasize );
If (! InternetGetCookieEx (url, null, cookieData, ref datasize, 0x00002000, null ))
Return null;
}
Return cookieData. ToString ();
} I have previously written an article about the technical skills of C # HttpWebRequest ignoring the certificate method. Please refer to this Article. Thank you. I will not ignore the certificate method below.
No matter whether it is Get or Post with Cookie, the encoding problem is solved, the problem of ignoring encoding is completely achieved here.
Recently, I sorted out the above method. This class is equivalent to a SqlHelper class. I call it the HttpHelper class and it will be updated later. I hope you can support it more,
Let's share it with you. If it is not good, thank you for your comments and correction. I will not talk about the code anymore!
/// <Summary>
/// Class description: HttpHelps class, used for Http access, Post or Get access, direct access, Cookie-based, certificate-based, and other methods
/// Encoding Date:
/// </Summary>
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Net;
Using System. IO;
Using System. Text. RegularExpressions;
Using System. IO. Compression;
Public class HttpHelps
{
# Region predefined methods or changes
// Default encoding
Private Encoding encoding = Encoding. Default;
// The HttpWebRequest object is used to initiate a request.
Private HttpWebRequest request = null;
// Obtain the data objects that affect the stream
Private HttpWebResponse response = null;
// Read the object of the stream
Private StreamReader reader = null;
// Data object to be returned
Private string returnData = "String Error ";
/// <Summary>
/// Obtain the page data based on the input data
/// </Summary>
/// <Param name = "strPostdata"> either the NUll or NUll string can be passed in Post mode or get mode. </param>
/// <Returns> string-type response data </returns>
Private string GetHttpRequestData (string strPostdata)
{
Try
{
// Supports page Jump. The query result will be the page after the jump.
Request. AllowAutoRedirect = true;
// Verify whether data is passed in when the result is obtained
If (! String. IsNullOrEmpty (strPostdata) & request. Method. Trim (). ToLower (). Contains ("post "))
{
Byte [] buffer = encoding. GetBytes (strPostdata );
Request. ContentLength = buffer. Length;
Request. GetRequestStream (). Write (buffer, 0, buffer. Length );
}
# Region get the Request response
Using (response = (HttpWebResponse) request. GetResponse ())
{
// From here on, we will ignore encoding.
If (encoding = null)
{
MemoryStream _ stream = new MemoryStream ();
If (response. ContentEncoding! = Null & response. ContentEncoding. Equals ("gzip", StringComparison. InvariantCultureIgnoreCase ))
{
// Start reading the stream and set the encoding method
New GZipStream (response. GetResponseStream (), CompressionMode. Decompress). CopyTo (_ stream, 10240 );
}
Else
{
Response. GetResponseStream (). CopyTo (_ stream, 10240 );
}
Byte [] RawResponse = _ stream. ToArray ();
String temp = Encoding. Default. GetString (RawResponse, 0, RawResponse. Length );
// <Meta (.*?) Charset ([\ s]?) = [^>] (. *?)>
Match meta = Regex. match (temp, "<meta ([^ <] *) charset = ([^ <] *) [\" '] ", RegexOptions. ignoreCase | RegexOptions. multiline );
String charter = (meta. Groups. Count> 2 )? Meta. Groups [2]. Value: string. Empty;
Charter = charter. Replace ("\" ", string. Empty). Replace (" '", string. Empty). Replace ("; ", string. Empty );
If (charter. Length> 0)
{
Encoding = Encoding. GetEncoding (charter );
}
Else
{
If (response. CharacterSet. ToLower (). Trim () = "iso-8859-1 ")
{
Encoding = Encoding. GetEncoding ("gbk ");
}
Else
{
If (string. IsNullOrEmpty (response. CharacterSet. Trim ()))
{
Encoding = Encoding. UTF8;
}
Else
{
Encoding = Encoding. GetEncoding (response. CharacterSet );
}
}
}
ReturnData = encoding. GetString (RawResponse );
}
Else
{
If (response. ContentEncoding! = Null & response. ContentEncoding. Equals ("gzip", StringComparison. InvariantCultureIgnoreCase ))
{
// Start reading the stream and set the encoding method
Using (reader = new StreamReader (new GZipStream (response. GetResponseStream (), CompressionMode. Decompress), encoding ))
{
ReturnData = reader. ReadToEnd ();
}
}
Else
{
// Start reading the stream and set the encoding method
Using (reader = new StreamReader (response. GetResponseStream (), encoding ))
{
ReturnData = reader. ReadToEnd ();
}
}
}
}
# Endregion
}
Catch (Exception)
{
// Here is the error message returned when an exception occurs
ReturnData = "String Error ";
}
Return returnData. ToLower ();
}