简体   繁体   English

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

[英]How to send/receive SOAP request and response using C#?

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;
}

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." 我尝试了不同的代码来发送/接收肥皂响应,但都失败了"The remote server returned an error: (500) Internal Server Error." .

I can access the same service using SoapUI. 我可以使用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. 我在这个论坛上看到,我得到500错误的原因可能是错误的标题。 I verified the header, it seems to be ok. 我验证了标题,似乎没问题。 I would appreciate if someone can help. 如果有人可以帮忙,我将不胜感激。

Following is the sample SOAP request: 以下是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>

I used the above sample request to execute the method and it worked. 我使用上面的示例请求来执行该方法并且它工作正常。 Here is the Soap request that I passed: 这是我通过的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>

Edit: 编辑:

I have updated the code above in WebServiceCall that worked for .asmx service. 我在WebServiceCall中更新了上面的代码,该代码适用于.asmx服务。 But the same code didn't work for WCF service. 但是相同的代码不适用于WCF服务。 Why? 为什么?

The urls are different. 网址不同。

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

vs.

  • /acctinqsvc/portfolioinquiry.asmx

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. 首先解决此问题,就好像Web服务器无法解析您尝试POST的URL,您甚至不会开始处理请求所描述的操作。

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. 您应该只需要为ASMX根URL创建WebRequest,即: http://localhost/AccountSvc/DataInquiry.asmx ,并在SOAPAction头中指定所需的方法/操作。

The SOAPAction header values are different. SOAPAction标头值不同。

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

vs.

  • http://tempuri.org/GetMyName

You should be able to determine the correct SOAPAction by going to the correct ASMX URL and appending ?wsdl 您应该能够通过转到正确的ASMX URL并附加?wsdl来确定正确的SOAPAction

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 . <wsdl:operation> <soap:operation>标记下应该有一个<soap:operation>标记,它与您尝试执行的操作相匹配,该标记似乎是GetMyName

There is no XML declaration in the request body that includes your SOAP XML. 请求正文中没有包含SOAP XML的XML声明。

You specify text/xml in the ContentType of your HttpRequest and no charset. 您在HttpRequest的ContentType中指定text/xml而不指定charset。 Perhaps these default to us-ascii , but there's no telling if you aren't specifying them! 也许这些默认为us-ascii ,但不知道你是不是在指定它们!

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 SoapUI创建的XML包含一个XML声明,它指定utf-8的编码,它也匹配提供给HTTP请求的Content-Type,它是: text/xml; charset=utf-8 text/xml; charset=utf-8

Hope that helps! 希望有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM