当尝试在一个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)。