尝试发送HTTPS请求并获得“接收时发生意外错误”

时间:2019-11-20 14:30:41

标签: c# https ssl-certificate

我们目前有一个第三方客户端,我们也发送一些XML。然后我们得到下载文件的回报。首先,我必须生成一个CSR,我们将它们提供给他们,然后他们给了我们一个pb7文件,作为回报,我将其安装在服务器上并与私钥配对。我有一个C#测试,尝试使用GetRequestStream发送POST请求时,该测试连续失败。我尝试将其设置为TL 1.2,并确保在服务器上启用了它。端口443已打开并且正在服务器上侦听。我想要的证书已发送,因此我不确定为什么我无法使请求通过。导致问题的代码如下。 WebResponse是RecieveFailure。我也进行了wireshark跟踪,可以看到它通过http从他们的服务器连接到我们的服务器,但是当我们的tcp转到他们的服务器时,它将立即失败。

private const string DTTM_PATTERN = "yyyy-MM-ddTHH:mm:ss.FZ";
        private const string SS_API_URL = "https://www.example.com/v_6?id=";
        private static string Proxy_Address = "My.Domain.Com";

        static void Main(string[] args)
        {

        var messageID = Guid.NewGuid().ToString().Replace("-", "");
           var currDate = DateTime.UtcNow.ToString(DTTM_PATTERN);



            string xmlMessage =
          @"The XML is here"
            string url = SS_API_URL + messageID;

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
            store.Open(OpenFlags.ReadOnly);
            X509Certificate2Collection collection = 
            store.Certificates.Find(X509FindType.FindBySubjectName, "my.certifcate.com", true);      
            request.ClientCertificates = collection;
            byte[] requestInFormOfBytes = 
            System.Text.Encoding.ASCII.GetBytes(xmlMessage);
            request.Method = "POST";
            request.ContentType = "text/xml;charset=utf-8";
            request.ContentLength = requestInFormOfBytes.Length;
            request.Proxy = new WebProxy(Proxy_Address, 443); 
            request.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            try
            {
               //The error appears here on request.GetRequestStream
                using (var requestStream = request.GetRequestStream())
                {
                    requestStream.Write(requestInFormOfBytes, 0, requestInFormOfBytes.Length);
                    requestStream.Close();
                }

                }
            catch (WebException webExcp)
            {
                // If you reach this point, an exception has been caught.  
                Console.WriteLine("A WebException has been caught.");
                // Write out the WebException message.  
                Console.WriteLine(webExcp.ToString());
                // Get the WebException status code.  
                WebExceptionStatus status = webExcp.Status;
                // If status is WebExceptionStatus.ProtocolError,   
                //   there has been a protocol error and a WebResponse   
                //   should exist. Display the protocol error.  
                if (status == WebExceptionStatus.ProtocolError)
                {
                    Console.Write("The server returned protocol error ");
                    // Get HttpWebResponse so that you can check the HTTP status code.  
                    HttpWebResponse httpResponse = (HttpWebResponse)webExcp.Response;
                    Console.WriteLine((int)httpResponse.StatusCode + " - "
                       + httpResponse.StatusCode);
                }
            }

0 个答案:

没有答案