0

我正在向 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 ----------

我该如何解决?

4

2 回答 2

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>
于 2012-11-14T13:12:21.133 回答
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 操作。您可以通过以下方式添加它:

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

于 2012-11-14T13:16:50.393 回答