如何使用 C# 发送/接收 SOAP 请求和响应? [英] How to send/receive SOAP request and response using C#?

查看:113
本文介绍了如何使用 C# 发送/接收 SOAP 请求和响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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响应,但都以相同的远程服务器返回错误:(500)内部服务器错误.".

I tried different code to send/receive soap responses but all fail with the same "The remote server returned an error: (500) Internal Server Error.".

我可以使用 SoapUI 访问相同的服务.我也可以调用该方法.我在这个论坛上读到,我收到 500 错误的原因可能是错误的标题.我验证了标题,似乎没问题.如果有人可以提供帮助,我将不胜感激.

I can access the same service using SoapUI. Am able to invoke the method too. I read in this forum that the reason why am I getting 500 error could be wrong header. I verified the header, it seems to be ok. I would appreciate if someone can help.

以下是示例 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 请求:

I used the above sample request to execute the method and it worked. Here is the Soap request that I passed:

<?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 服务的 WebServiceCall 中的上述代码.但是相同的代码不适用于 WCF 服务.为什么?

I have updated the code above in WebServiceCall that worked for .asmx service. But the same code didn't work for WCF service. Why?

推荐答案

网址不同.

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

对比

  • /acctinqsvc/portfolioinquiry.asmx

首先解决这个问题,就像网络服务器无法解析您尝试 POST 到的 URL 一样,您甚至不会开始处理您的请求所描述的操作.

Resolve this issue first, as if the web server cannot resolve the URL you are attempting to POST to, you won't even begin to process the actions described by your request.

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

You should only need to create the WebRequest to the ASMX root URL, ie: http://localhost/AccountSvc/DataInquiry.asmx, and specify the desired method/operation in the SOAPAction header.

SOAPAction 标头值不同.

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

对比

  • http://tempuri.org/GetMyName

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

You should be able to determine the correct SOAPAction by going to the correct ASMX URL and appending ?wsdl

标签下应该有一个 标签与您尝试执行的操作相匹配,这似乎是成为 GetMyName.

There should be a <soap:operation> tag underneath the <wsdl:operation> tag that matches the operation you are attempting to execute, which appears to be GetMyName.

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

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

You specify text/xml in the ContentType of your HttpRequest and no charset. Perhaps these default to us-ascii, but there's no telling if you aren't specifying them!

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

The SoapUI created XML includes an XML declaration that specifies an encoding of utf-8, which also matches the Content-Type provided to the HTTP request which is: text/xml; charset=utf-8

希望有帮助!

这篇关于如何使用 C# 发送/接收 SOAP 请求和响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆