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

查看:3288
本文介绍了如何发送/使用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)内部服务器错误

我可以访问相同。服务使用了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>



修改



我已经更新上面WebServiceCall代码,对于服务的.asmx工作。但同样的代码没有对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?

推荐答案

的URL是不同的。


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

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

VS


  • /acctinqsvc/portfolioinquiry.asmx

  • /acctinqsvc/portfolioinquiry.asmx

先解决这个问题,因为如果Web服务器无法解析您试图张贴到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.

您应该只需要创建的WebRequest到ASMX根URL,例如: 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/ +方法名

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

VS


  • http://tempuri.org/GetMyName

  • http://tempuri.org/GetMyName

您应该可以通过将正确的ASMX URL和追加,以确定正确的SOAPAction ?WSDL

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

应该有<肥皂:运转> 下方标记< 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.

有在包括请求主体没有XML声明您的SOAP XML。

您指定文本/ XML 在你的HttpRequest没有的字符集的ContentType 。也许这些默认为 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这是一个编码:文本/ XML;字符集= 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天全站免登陆