HttpWebRequest.GetRequestStream()-错误基础连接已关闭:发送中发生意外错误

时间:2019-05-24 13:22:46

标签: c# httpwebrequest

当尝试在一个Web服务上发送xml请求以进行响应时,HttpWebRequest.GetRequest()出现错误。邮递员的同样要求。请指导正确的方向以解决此问题。下面是XML正文文本和代码。

    string xmlMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" 
"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" 
  "<soap:Body>" 
"<GetDetail xmlns=\"http://tempuri.org/\">" 
  "<strNo>22101</strNo> <baCode></baCode> <authkey>xxxx</authkey><source>xxxx</source>" 
"</GetDetail>" "</soap:Body>" "</soap:Envelope>";


        byte[] requestInFormOfBytes = System.Text.Encoding.ASCII.GetBytes(xmlMessage);
        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("https://xxxxxxx.xx/xxxx/GetDetail");
        webRequest.Method = "POST";
        webRequest.ContentType = "application/xml";
        webRequest.Headers.Add("SOAPAction", "http://tempuri.org/GetDetail");
        webRequest.Headers.Add("Client-Id", "XXXXXXXXXXXXX");
        webRequest.Headers.Add("Client-Secret", "XXXXXXXXXXXXXXXXX");
        webRequest.Accept = "application/xml";
        webRequest.ContentLength = requestInFormOfBytes.Length;

        ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;

在此行给出错误。

流requestStream = webRequest.GetRequestStream();

 requestStream.Write(requestInFormOfBytes, 0, requestInFormOfBytes.Length);
        requestStream.Close();

        HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
        StreamReader respStream = new StreamReader(response.GetResponseStream(), System.Text.Encoding.Default);
        string receivedResponse = respStream.ReadToEnd();
        Console.WriteLine(receivedResponse);
        respStream.Close();
        response.Close();

感谢所有人提供正确的指导,其解决方法是(1)使用FrameWork 4.6进行测试 (2)使用SecurityProtocoltype.Tls12(请参阅-https://stackoverflow.com/a/32789483/5694613)。

0 个答案:

没有答案