如何使用 HttpWebRequest 实现 SSL?
How to implement SSL with HttpWebRequest?
我将从 https 网页获取信息。但是下载的数据是编码的,看不清。
\u001f�\b[=11=][=11=][=11=][=11=][=11=][=11=][=11=]�iw۸�0�Y�\u0015\u0018��(y�z��,v���rܝ\u0019�v�N/�s��P\",3�H]��r��\u007f\u007fQ�\b���N���\u0011Il�B��[=11=]\u0014�^�@�ŋ\u0017����\u007f���Gt��\u0003t����S\u001f9k\u001b\u001b\u007fl�76\u000e/\u000fџ�^~>F[��2v��O�(t����'\u000er��t���qww�~��\u001eţ���{�k\v\n�ǵT)��}�\u001eR�\a�^�E��\u0018�.����?���\a�,vGc�A�(Lq�~p�hm�\u000e���a�߇��>�\u001dGA�b\u001f�'~�\u0013���\u0016�\n\bB�/��\K\u001f&X)���t\u0003z����n���×ˣ��\u000e\"�YkjOh\u001f۲��[=11=]�]\���\rY?\u001b\u0015u�����%��[=11=]w�o��{����\u000fo�u��>8��\u001b�$�\b\"��_\lv6�v6�ެ�o\u000e�q��I҇[=11=]'�
如何将 SSL 证书放入 HttpWebRequest class?
class MyHttpWebRequest : IDisposable
{
private static int Count = 0;
private HttpWebRequest request;
private Stream dataStream;
private string Status { get; set; }
private string userAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36";
private string accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8";
private string acceptLanguage = "Accept-Language: en-US,en;q=0.9";
private string acceptEncoding = "Accept-Encoding: gzip, deflate, br";
private string host;
private X509Certificate2 clientCertificates;
private String referer { get; set; } = "";
private CookieCollection cookieCollection { get; set; } = null;
public MyHttpWebRequest()
{
Count++;
clientCertificates = new X509Certificate2(@"mahan.cer");
//clientCertificates = X509Certificate.CreateFromCertFile(@"mahan.cer");//(@"LocalAuthority.crt");
//ServicePointManager.Expect100Continue = true;
//ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
//ServicePointManager.Expect100Continue = false;
//ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
//WebRequestHandler handler = new WebRequestHandler();
//X509Certificate2 certificate = GetMyX509Certificate();
//handler.ClientCertificates.Add(certificate);
//HttpClient client = new HttpClient(handler);
}
public MyHttpWebRequest(string host) : this()
{
this.host = host;
}
~MyHttpWebRequest()
{
Count--;
}
public string GetResponse()
{
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (cookieCollection == null) cookieCollection = new CookieCollection();
cookieCollection.Add(response.Cookies);
this.Status = response.StatusDescription;
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
// Clean up the streams.
reader.Close();
//dataStream.Close();
response.Close();
//clientCertificates = new X509Certificate2(request.ServicePoint.Certificate);
this.Status = "Successful";
return responseFromServer;
}
catch (Exception ex)
{
this.Status = ex.Message;
return ex.ToString();
}
}
public string SetRequest(string url)
{
try
{
request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.UserAgent = userAgent;
request.Accept = accept;
request.Headers.Add(acceptLanguage);
request.Headers.Add(acceptEncoding);
request.KeepAlive = true;
request.CookieContainer = new CookieContainer();
if (cookieCollection != null)
request.CookieContainer.Add(cookieCollection);
request.ProtocolVersion = HttpVersion.Version11;
request.AllowAutoRedirect = false;
request.ContentType = "application/x-www-form-urlencoded";
request.Referer = referer;
referer = url;
request.Host = host;
//request.ClientCertificates.Clear();
request.ClientCertificates.Add(clientCertificates);
request.PreAuthenticate = true;
this.Status = "Successful";
}
catch (Exception ex)
{
this.Status = ex.ToString();
}
return this.ToString();
}
public string SetRequest(string url, string method)
{
try
{
request = (HttpWebRequest)WebRequest.Create(url);
if (method.Equals("GET") || method.Equals("POST"))
{
request.Method = method;
}
else
{
throw new Exception("Invalid Method Type");
}
request.UserAgent = userAgent;
request.Accept = accept;
request.Headers.Add(acceptLanguage);
request.Headers.Add(acceptEncoding);
request.KeepAlive = true;
request.CookieContainer = new CookieContainer();
if (cookieCollection != null)
request.CookieContainer.Add(cookieCollection);
request.ProtocolVersion = HttpVersion.Version11;
request.AllowAutoRedirect = false;
request.ContentType = "application/x-www-form-urlencoded";
request.Referer = referer;
referer = url;
request.Host = host;
//request.ClientCertificates.Clear();
request.ClientCertificates.Add(clientCertificates);
request.PreAuthenticate = true;
this.Status = "Successful";
}
catch (Exception ex)
{
this.Status = ex.ToString();
}
return this.ToString();
}
public string SetRequest(string url, string method, string data)
{
try
{
request = (HttpWebRequest)WebRequest.Create(url);
if (method.Equals("GET") || method.Equals("POST"))
{
request.Method = method;
}
else
{
throw new Exception("Invalid Method Type");
}
request.UserAgent = userAgent;
request.Accept = accept;
request.Headers.Add(acceptLanguage);
request.Headers.Add(acceptEncoding);
request.KeepAlive = true;
request.CookieContainer = new CookieContainer();
if (cookieCollection != null)
request.CookieContainer.Add(cookieCollection);
request.ProtocolVersion = HttpVersion.Version11;
request.AllowAutoRedirect = false;
request.ContentType = "application/x-www-form-urlencoded";
request.Referer = referer;
referer = url;
request.Host = host;
//request.ClientCertificates.Clear();
request.ClientCertificates.Add(clientCertificates);
request.PreAuthenticate = true;
byte[] byteArray = Encoding.UTF8.GetBytes(data);
request.ContentLength = byteArray.Length;
dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
this.Status = "Successful";
}
catch (Exception ex)
{
this.Status = ex.Message;
}
return this.Status;
}
public string SetRequest(string url, string method, string data, string contentType)
{
try
{
request = (HttpWebRequest)WebRequest.Create(url);
if (method.Equals("GET") || method.Equals("POST"))
{
request.Method = method;
}
else
{
throw new Exception("Invalid Method Type");
}
request.UserAgent = userAgent;
request.Accept = accept;
request.Headers.Add(acceptLanguage);
request.Headers.Add(acceptEncoding);
request.KeepAlive = true;
request.CookieContainer = new CookieContainer();
if (cookieCollection != null)
request.CookieContainer.Add(cookieCollection);
request.ProtocolVersion = HttpVersion.Version11;
request.AllowAutoRedirect = false;
request.ContentType = contentType;
request.Referer = referer;
referer = url;
request.Host = host;
//request.ClientCertificates.Clear();
request.ClientCertificates.Add(clientCertificates);
request.PreAuthenticate = true;
byte[] byteArray = Encoding.UTF8.GetBytes(data);
request.ContentLength = byteArray.Length;
dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
this.Status = "Successful";
}
catch (Exception ex)
{
this.Status = ex.Message;
}
return this.Status;
}
public void Dispose()
{
request.Abort();
request = null;
dataStream.Close();
dataStream.Dispose();
dataStream = null;
}
}
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://yourwebsite.nld");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
response.Close();
X509Certificate certV1 = request.ServicePoint.Certificate;
证书是 X509Certificate 而不是 X509Certificate2。
要转换添加
X509Certificate2 certV2 = new X509Certificate2(certV1);
How to get the SSL certificate into the HttpWebRequest class?
您的问题与 SSL 完全无关。
private string acceptEncoding = "Accept-Encoding: gzip, deflate, br";
...
request.Headers.Add(acceptEncoding);
使用此代码,您可以明确告诉服务器您将支持各种内容压缩算法。只是,这是一个谎言,因为您在阅读响应时没有处理压缩。
\u001f�\b[=15=][=15=][=15=][=15=][=15=][=15=][=15=]
这看起来是 gzip 数据流的开头。它以十六进制 \x1f\x8b 开头,但您将其错误地解释为 UTF-8 而不是。
修复此部分的最简单方法是从您的请求中删除 Accept-Encoding
或使用 Accept-Encoding: identity
表示您不接受任何压缩。这样一个表现良好的服务器将在没有任何压缩的情况下向您发送正文。
我将从 https 网页获取信息。但是下载的数据是编码的,看不清。
\u001f�\b[=11=][=11=][=11=][=11=][=11=][=11=][=11=]�iw۸�0�Y�\u0015\u0018��(y�z��,v���rܝ\u0019�v�N/�s��P\",3�H]��r��\u007f\u007fQ�\b���N���\u0011Il�B��[=11=]\u0014�^�@�ŋ\u0017����\u007f���Gt��\u0003t����S\u001f9k\u001b\u001b\u007fl�76\u000e/\u000fџ�^~>F[��2v��O�(t����'\u000er��t���qww�~��\u001eţ���{�k\v\n�ǵT)��}�\u001eR�\a�^�E��\u0018�.����?���\a�,vGc�A�(Lq�~p�hm�\u000e���a�߇��>�\u001dGA�b\u001f�'~�\u0013���\u0016�\n\bB�/��\K\u001f&X)���t\u0003z����n���×ˣ��\u000e\"�YkjOh\u001f۲��[=11=]�]\���\rY?\u001b\u0015u�����%��[=11=]w�o��{����\u000fo�u��>8��\u001b�$�\b\"��_\lv6�v6�ެ�o\u000e�q��I҇[=11=]'�
如何将 SSL 证书放入 HttpWebRequest class?
class MyHttpWebRequest : IDisposable
{
private static int Count = 0;
private HttpWebRequest request;
private Stream dataStream;
private string Status { get; set; }
private string userAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36";
private string accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8";
private string acceptLanguage = "Accept-Language: en-US,en;q=0.9";
private string acceptEncoding = "Accept-Encoding: gzip, deflate, br";
private string host;
private X509Certificate2 clientCertificates;
private String referer { get; set; } = "";
private CookieCollection cookieCollection { get; set; } = null;
public MyHttpWebRequest()
{
Count++;
clientCertificates = new X509Certificate2(@"mahan.cer");
//clientCertificates = X509Certificate.CreateFromCertFile(@"mahan.cer");//(@"LocalAuthority.crt");
//ServicePointManager.Expect100Continue = true;
//ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
//ServicePointManager.Expect100Continue = false;
//ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
//WebRequestHandler handler = new WebRequestHandler();
//X509Certificate2 certificate = GetMyX509Certificate();
//handler.ClientCertificates.Add(certificate);
//HttpClient client = new HttpClient(handler);
}
public MyHttpWebRequest(string host) : this()
{
this.host = host;
}
~MyHttpWebRequest()
{
Count--;
}
public string GetResponse()
{
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (cookieCollection == null) cookieCollection = new CookieCollection();
cookieCollection.Add(response.Cookies);
this.Status = response.StatusDescription;
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
// Clean up the streams.
reader.Close();
//dataStream.Close();
response.Close();
//clientCertificates = new X509Certificate2(request.ServicePoint.Certificate);
this.Status = "Successful";
return responseFromServer;
}
catch (Exception ex)
{
this.Status = ex.Message;
return ex.ToString();
}
}
public string SetRequest(string url)
{
try
{
request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.UserAgent = userAgent;
request.Accept = accept;
request.Headers.Add(acceptLanguage);
request.Headers.Add(acceptEncoding);
request.KeepAlive = true;
request.CookieContainer = new CookieContainer();
if (cookieCollection != null)
request.CookieContainer.Add(cookieCollection);
request.ProtocolVersion = HttpVersion.Version11;
request.AllowAutoRedirect = false;
request.ContentType = "application/x-www-form-urlencoded";
request.Referer = referer;
referer = url;
request.Host = host;
//request.ClientCertificates.Clear();
request.ClientCertificates.Add(clientCertificates);
request.PreAuthenticate = true;
this.Status = "Successful";
}
catch (Exception ex)
{
this.Status = ex.ToString();
}
return this.ToString();
}
public string SetRequest(string url, string method)
{
try
{
request = (HttpWebRequest)WebRequest.Create(url);
if (method.Equals("GET") || method.Equals("POST"))
{
request.Method = method;
}
else
{
throw new Exception("Invalid Method Type");
}
request.UserAgent = userAgent;
request.Accept = accept;
request.Headers.Add(acceptLanguage);
request.Headers.Add(acceptEncoding);
request.KeepAlive = true;
request.CookieContainer = new CookieContainer();
if (cookieCollection != null)
request.CookieContainer.Add(cookieCollection);
request.ProtocolVersion = HttpVersion.Version11;
request.AllowAutoRedirect = false;
request.ContentType = "application/x-www-form-urlencoded";
request.Referer = referer;
referer = url;
request.Host = host;
//request.ClientCertificates.Clear();
request.ClientCertificates.Add(clientCertificates);
request.PreAuthenticate = true;
this.Status = "Successful";
}
catch (Exception ex)
{
this.Status = ex.ToString();
}
return this.ToString();
}
public string SetRequest(string url, string method, string data)
{
try
{
request = (HttpWebRequest)WebRequest.Create(url);
if (method.Equals("GET") || method.Equals("POST"))
{
request.Method = method;
}
else
{
throw new Exception("Invalid Method Type");
}
request.UserAgent = userAgent;
request.Accept = accept;
request.Headers.Add(acceptLanguage);
request.Headers.Add(acceptEncoding);
request.KeepAlive = true;
request.CookieContainer = new CookieContainer();
if (cookieCollection != null)
request.CookieContainer.Add(cookieCollection);
request.ProtocolVersion = HttpVersion.Version11;
request.AllowAutoRedirect = false;
request.ContentType = "application/x-www-form-urlencoded";
request.Referer = referer;
referer = url;
request.Host = host;
//request.ClientCertificates.Clear();
request.ClientCertificates.Add(clientCertificates);
request.PreAuthenticate = true;
byte[] byteArray = Encoding.UTF8.GetBytes(data);
request.ContentLength = byteArray.Length;
dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
this.Status = "Successful";
}
catch (Exception ex)
{
this.Status = ex.Message;
}
return this.Status;
}
public string SetRequest(string url, string method, string data, string contentType)
{
try
{
request = (HttpWebRequest)WebRequest.Create(url);
if (method.Equals("GET") || method.Equals("POST"))
{
request.Method = method;
}
else
{
throw new Exception("Invalid Method Type");
}
request.UserAgent = userAgent;
request.Accept = accept;
request.Headers.Add(acceptLanguage);
request.Headers.Add(acceptEncoding);
request.KeepAlive = true;
request.CookieContainer = new CookieContainer();
if (cookieCollection != null)
request.CookieContainer.Add(cookieCollection);
request.ProtocolVersion = HttpVersion.Version11;
request.AllowAutoRedirect = false;
request.ContentType = contentType;
request.Referer = referer;
referer = url;
request.Host = host;
//request.ClientCertificates.Clear();
request.ClientCertificates.Add(clientCertificates);
request.PreAuthenticate = true;
byte[] byteArray = Encoding.UTF8.GetBytes(data);
request.ContentLength = byteArray.Length;
dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
this.Status = "Successful";
}
catch (Exception ex)
{
this.Status = ex.Message;
}
return this.Status;
}
public void Dispose()
{
request.Abort();
request = null;
dataStream.Close();
dataStream.Dispose();
dataStream = null;
}
}
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://yourwebsite.nld");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
response.Close();
X509Certificate certV1 = request.ServicePoint.Certificate;
证书是 X509Certificate 而不是 X509Certificate2。 要转换添加
X509Certificate2 certV2 = new X509Certificate2(certV1);
How to get the SSL certificate into the HttpWebRequest class?
您的问题与 SSL 完全无关。
private string acceptEncoding = "Accept-Encoding: gzip, deflate, br"; ... request.Headers.Add(acceptEncoding);
使用此代码,您可以明确告诉服务器您将支持各种内容压缩算法。只是,这是一个谎言,因为您在阅读响应时没有处理压缩。
\u001f�\b[=15=][=15=][=15=][=15=][=15=][=15=][=15=]
这看起来是 gzip 数据流的开头。它以十六进制 \x1f\x8b 开头,但您将其错误地解释为 UTF-8 而不是。
修复此部分的最简单方法是从您的请求中删除 Accept-Encoding
或使用 Accept-Encoding: identity
表示您不接受任何压缩。这样一个表现良好的服务器将在没有任何压缩的情况下向您发送正文。