我正在向 ASMX Web 服务发出如下请求 -
private HttpWebResponse SendSoap12Msg(string url, string method,
Dictionary<string, string> KeyValue)
{
StringBuilder SoapMessage = new StringBuilder();
SoapMessage.Append("<?xml version='1.0' encoding='utf-8'?>");
SoapMessage.Append(@"<soap12:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'");
SoapMessage.Append(@" xmlns:xsd='http://www.w3.org/2001/XMLSchema'>");
SoapMessage.Append(@" xmlns:soap12='http://www.w3.org/2003/05/soap-envelope'");
//SoapMessage.Append(@" xmlns:soap12='http://schemas.xmlsoap.org/wsdl/soap12/'");
SoapMessage.Append("<soap12:Body>");
SoapMessage.Append("<");
SoapMessage.Append(method);
SoapMessage.Append(@" xmlns='http://tempurl.org/'>");
foreach (KeyValuePair<string, string> kvp in KeyValue)
{
SoapMessage.Append("<");
SoapMessage.Append(kvp.Key);
SoapMessage.Append(">");
SoapMessage.Append(kvp.Value);
SoapMessage.Append("</");
SoapMessage.Append(kvp.Key);
SoapMessage.Append(">");
}
SoapMessage.Append("</");
SoapMessage.Append(method);
SoapMessage.Append(">");
SoapMessage.Append("</soap12:Body>");
SoapMessage.Append("</soap12:Envelope>");
// Build HttpWebRequest
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(url));
request.Method = "POST";
request.ProtocolVersion = HttpVersion.Version11;
request.ContentType = "application/soap+xml; charset=\"utf-8\"";
//request.Accept = "application/soap+xml";
// Send SOAP Envelope
byte[] data = Encoding.UTF8.GetBytes(SoapMessage.ToString());
request.ContentLength = data.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(data, 0, data.Length);
requestStream.Close();
return (HttpWebResponse ) request.GetResponse();
}
但是,每当发送请求时,我都会收到500 - Internal Server Error
一个响应。使用这些深入挖掘异常 -
catch (WebException ex)
{
Response.ContentType = "text/html";
Response.Write("---------- Start: A WebException occured ----------<br />");
Response.Write("Returned Content Type: " + ex.Response.ContentType);
Response.Write("<br />");
Response.Write("Is From Cache: " + ex.Response.IsFromCache);
Response.Write("<br />");
Response.Write("Response URI: " + ex.Response.ResponseUri.ToString());
Response.Write("<br />");
Response.Write("ToString: " + ex.Response.ToString());
Response.Write("<br />");
Response.Write("ReadToEnd: " +
new StreamReader(ex.Response.GetResponseStream()).ReadToEnd());
Response.Write("<br />");
Response.Write("---------- End: A WebException occured ----------");
}
我得到以下输出 -
---------- Start: A WebException occured ----------
Returned Content Type: application/soap+xml; charset=utf-8
Is From Cache: False
Response URI: (the target uri, as expected)
ToString: System.Net.HttpWebResponse
ReadToEnd: soap:ReceiverServer was unable to process request. ---> 'soap12' is an undeclared prefix. Line 1, position 40.
---------- End: A WebException occured ----------
我该如何解决?