C#WebRequest(400)第二次通话时出现错误请求

时间:2016-04-27 05:56:01

标签: c# ssl httpwebrequest cache-control

我正在尝试使用程序检查我们的SSL证书,但我有一个问题;我不能使用第二个HttpWebRequest因为我不知道的东西。我认为这是一个缓存问题,并使用了我所知道的所有无缓存代码。正如您所看到的,服务器位于我们的私有子网中,并且1台服务器具有大量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

InternalStatus:RequestFatal

1 个答案:

答案 0 :(得分:0)

我解决了我的问题。问题是保持活动属性,因为第二个SSL连接没有正确初始化。您必须将此行添加到您的请求中。

        request.KeepAlive = false;