如何使用C#发送/接收SOAP请求和响应?

23
private static string WebServiceCall(string methodName)        
{
    WebRequest webRequest = WebRequest.Create("http://localhost/AccountSvc/DataInquiry.asmx");
    HttpWebRequest httpRequest = (HttpWebRequest)webRequest;             
    httpRequest.Method = "POST";             
    httpRequest.ContentType = "text/xml; charset=utf-8";
    httpRequest.Headers.Add("SOAPAction: http://tempuri.org/" + methodName);
    httpRequest.ProtocolVersion = HttpVersion.Version11;
    httpRequest.Credentials = CredentialCache.DefaultCredentials;
    Stream requestStream = httpRequest.GetRequestStream();              
    //Create Stream and Complete Request             
    StreamWriter streamWriter = new StreamWriter(requestStream, Encoding.ASCII);

    StringBuilder soapRequest = new StringBuilder("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"");
    soapRequest.Append(" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" ");
    soapRequest.Append("xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body>");
    soapRequest.Append("<GetMyName xmlns=\"http://tempuri.org/\"><name>Sam</name></GetMyName>");
    soapRequest.Append("</soap:Body></soap:Envelope>");

    streamWriter.Write(soapRequest.ToString());             
    streamWriter.Close();              
    //Get the Response    
    HttpWebResponse wr = (HttpWebResponse)httpRequest.GetResponse();
    StreamReader srd = new StreamReader(wr.GetResponseStream()); 
    string resulXmlFromWebService = srd.ReadToEnd(); 
    return resulXmlFromWebService;
}

我尝试了不同的代码来发送/接收SOAP响应,但都失败并显示相同的错误信息"The remote server returned an error: (500) Internal Server Error.".

我可以使用SoapUI访问同样的服务。我也能够调用该方法。我在这个论坛上读到,我之所以会收到500错误的原因可能是错误的头信息。我验证了头部,它似乎没问题。如果有人能帮忙解决,我将不胜感激。

以下是示例SOAP请求:

POST /AccountSvc/DataInquiry.asmx HTTP/1.1
Host: abc.def.gh.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/GetMyName"

<?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>
    <GetMyName xmlns="http://tempuri.org/">
      <name>string</name>
    </GetMyName>
  </soap:Body>
</soap:Envelope>

我使用了上述示例请求来执行该方法,并且它起作用了。这是我传递的 Soap 请求:

<?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><GetMyName xmlns="http://tempuri.org/"><name>Sam</name></GetMyName></soap:Body></soap:Envelope>

编辑:

我已经更新了上面的代码,可以在使用 .asmx 服务时正常工作。但是同样的代码在使用 WCF 服务时却不起作用。为什么?


3
更多提示:不要使用字符串操作来创建 XML。始终使用其中一个 XML API,如 LINQ to XML 或 XmlWriter。此外,Streams、StreamReader/Writers 和 WebResponse 需要放在 using 块中,以便在发生异常时不会泄漏资源。 - John Saunders
没有看到你的代码,很难说。我猜测你可能没有指定足够的行为。这个链接可能对你有用,因为它描述了WCF端点/服务的SOAP配置: https://dev59.com/dXVC5IYBdhLWcg3wykQt#186695 - rrhartjr
1个回答

21

链接不同。

  • http://localhost/AccountSvc/DataInquiry.asmx

相对于

  • /acctinqsvc/portfolioinquiry.asmx

首先解决此问题,如果Web服务器无法解析您尝试POST到的URL,则您甚至不会开始处理请求描述的操作。

您只需要创建到ASMX根URL的WebRequest ,即:http://localhost/AccountSvc/DataInquiry.asmx,并在SOAPAction标头中指定所需的方法/操作。

SOAPAction标头值不同。

  • http://localhost/AccountSvc/DataInquiry.asmx/ + methodName

相对于

  • http://tempuri.org/GetMyName

您应该能够通过转到正确的ASMX URL并附加?wsdl来确定正确的SOAPAction。

在匹配您要执行的操作的<wsdl:operation>标签下面,应该有一个<soap:operation>标签,看起来是GetMyName

请求正文中没有包含SOAP XML的XML声明。

您在HttpRequest的ContentType中指定了text/xml,但没有字符集。也许默认为us-ascii,但如果您没有指定它们,就无法确定!

SoapUI创建的XML包括一个XML声明,该声明指定了utf-8编码,这也与提供给HTTP请求的Content-Type匹配,即:text/xml; charset=utf-8

希望有所帮助!


抱歉回复晚了。我已更新代码,现在它可以工作了。谢谢!你知道为什么同样的代码在WCF服务中无法运行吗?起初,它给了我错误提示:(415)不能处理消息,因为内容类型与预期不符。将其更改为application/soap+xml; charset=utf-8"。然后它又显示“400 Bad Request”。 - Sri Reddy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接