我将从https网页获取信息。但是下载的数据已编码且难以辨认。


  \u001f�\ b \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0�iw۸�0�Y� \ u0015 \u0018��(y�z��,v���r\u0019�v�N /�sP\“,3�H]��r��\ u007f \u007fQ�\b�N���\u0011Il�B��\ 0 \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 ~~ phm。\u000e���a�߇��>。\u001dGA�b\u001f�'~~ \ u0013 �\u0016�\ n \bB�/��\ K \ u001f&X)���t\u0003z����n���×ˣ��\ u000e \''。YkjOh \ u001f۲�� \\ 0。 ] \���\ rY?\ u001b \u0015u���%% \0w.o��{����\u000fo�u��>8��\u001b�$�\ b \“ ��_\lv6�v6�ެo \u000e�q��I҇\ 0'。


如何将SSL证书放入HttpWebRequest类?

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

最佳答案

如何将SSL证书放入HttpWebRequest类?


您的问题与SSL根本无关。


private string acceptEncoding = "Accept-Encoding: gzip, deflate, br";
...
        request.Headers.Add(acceptEncoding);



使用此代码,您可以明确告诉服务器您将支持各种内容压缩算法。只是,这是一个谎言,因为您在读取响应时不处理压缩。


  \u001f�\ b \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0


这看起来是gzip数据流的开始。它以十六进制\ x1f \ x8b开头,但是您误认为它不是UTF-8。

修复此部分的最简单方法是从请求中删除Accept-Encoding或使用Accept-Encoding: identity表示您不接受任何压缩。这样,行为良好的服务器将无需任何压缩即可将您的身体发送给您。

关于c# - 如何使用HttpWebRequest实现SSL?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48606437/

10-11 11:41