我正在向ASMX网络服务发出如下请求 -
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 ----------
我该如何解决?
答案 0 :(得分:1)
确保已将SOAP1.2的绑定添加到web.config中,并为该服务配置并启用它们。
<wsdl:binding name="ServiceSoap12" type="tns:ServiceSoap">
然后
<configuration>
<system.web>
<webServices>
<protocols>
<add name="HttpSoap12"/>
</protocols>
</webServices>
</system.web>
</configuration>
答案 1 :(得分:1)
我正在构建的SOAP请求中唯一看到的是方法名称之后和参数之前的<request>
标记。省略它有时会产生这样的错误,或者将请求转换为WS实体失败。
尝试以下内容(为简洁起见省略了一些内容):
SoapMessage.Append(String.Format(@"<{0} xmlns='http://tempurl.org/'>", method));
SoapMessage.Append("<request>"); // Line added
foreach (KeyValuePair<string, string> kvp in KeyValue)
{
SoapMessage.Append(String.Format("<{0}>", kvp.Key));
SoapMessage.Append(kvp.Value);
SoapMessage.Append(String.Format("</{0}>", kvp.Key));
}
SoapMessage.Append("</request>"); // Line added
SoapMessage.Append(String.Format("</{0}>", method));
巧合的是,我遇到了我们客户的这个问题,缺少请求标签就是原因。在某些情况下,你可以安全地省略它,但有时它显然是强制性的。我还添加了String.Format
的使用,这使得代码更简短,更容易理解。
修改强> 我唯一看到的缺点是您在请求标头中设置了SOAP action。您可以通过以下方式添加它:
request.Headers.Add("SOAPAction:", "http://tempurl.org/YourMethodname");
// You might need to try some variations.
// Or replacing tempurl.org with the actual domain.
如果您谷歌搜索“C#HttpWebRequest SoapAction”,您会发现更多人获得错误500;特别是在请求中省略它时。
另请参阅此博客条目与您尝试的几乎完全相同:http://mikehadlow.blogspot.nl/2006/05/making-raw-web-service-calls-with.html