簡體   English   中英

C#WebRequest(400)第二次調用時請求錯誤

[英]C# WebRequest (400) Bad Request on second call

我正在嘗試使用程序檢查我們的SSL證書,但是我遇到了問題; 由於某些原因,我無法使用第二個HttpWebRequest。 我以為這是一個緩存問題,並使用了我所知道的所有無緩存代碼。 如您所見,服務器位於我們的專用子網中,其中一台服務器具有大量IIS應用程序。 因此,我將“ request.Host”變量更改為我們匹配的IIS綁定,以打開網站。 總是第二個無效,但第一個有效。 問題是什么?

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://" + "10.10.11.100");
            request.AllowAutoRedirect = false;
            request.Host = "www.xxx.com";
            request.Timeout = 30 * 1000;
            request.Headers.Add("Cache-Control", "no-cache");
            request.Referer = null;
            request.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                //retrieve the ssl cert and assign it to an X509Certificate object
                X509Certificate cert = request.ServicePoint.Certificate;
                //convert the X509Certificate to an X509Certificate2 object by passing it into the constructor
                X509Certificate2UI.DisplayCertificate(new X509Certificate2(cert));
            }
            HttpWebRequest request2 = (HttpWebRequest)WebRequest.Create("https://" + "10.10.11.100");
            request2.AllowAutoRedirect = false;
            request2.Host = "www.yyy.com";
            request2.Timeout = 30 * 1000;
            request2.Headers.Add("Cache-Control", "no-cache");
            request2.Referer = null;

            request2.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
            using (HttpWebResponse response2 = (HttpWebResponse)request2.GetResponse())
            {
                //retrieve the ssl cert and assign it to an X509Certificate object
                X509Certificate cert2 = request2.ServicePoint.Certificate;
                //convert the X509Certificate to an X509Certificate2 object by passing it into the constructor
                X509Certificate2UI.DisplayCertificate(new X509Certificate2(cert2));
            }

錯誤:

        The remote server returned an error: (400) Bad Request.

例外行:

using (HttpWebResponse response2 = (HttpWebResponse)request2.GetResponse()) 

狀態:ProtocolError

內部狀態:RequestFatal

我解決了我的問題。 問題是保持活動屬性,並且由於第二個SSL連接未正確初始化。 您必須將此行添加到您的請求中。

        request.KeepAlive = false;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM