繁体   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