C#SOAP调用一直返回500错误

时间:2017-01-17 10:13:43

标签: c# web-services soap

在做了一些调查并阅读了几篇关于它的帖子后,特别是这个:

我想出了以下代码:

RequestingXMLData = @"<?xml version=""1.0"" encoding=""UTF-8""?>
                    <soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:urn=""urn:iaso-com:cloud:mansvc:15.12.0"">
                        <soapenv:Header/>
                        <soapenv:Body>
                            <urn:Authenticate>
                                <request>
                                    <partnerName>XXX</partnerName>
                                    <userName>XXX</userName>
                                    <password>XXX</password>
                                </request>
                            </urn:Authenticate>
                        </soapenv:Body>
                    </soapenv:Envelope>";

//Retrieves XML Data from API link / service, optional to spit XML to a given file location
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(ApiCallPath);
request.Method = "POST";
request.ContentType = "text/xml;charset=\"utf-8\"";
request.ContentLength = RequestingXMLData.Length;
using (Stream webStream = request.GetRequestStream())
using (StreamWriter requestWriter = new StreamWriter(webStream, System.Text.Encoding.ASCII))
{
    requestWriter.Write(RequestingXMLData);
}

try
{
    WebResponse webResponse = request.GetResponse();
    using (Stream webStream = webResponse.GetResponseStream())
    {
        if (webStream != null)
        {
            using (StreamReader responseReader = new StreamReader(webStream))
            {
                response = responseReader.ReadToEnd();
            }
        }
    }
}
catch (WebException e)
{
    var resp = new StreamReader(e.Response.GetResponseStream()).ReadToEnd();

}

return response;

我一直在the WebResponse webResponse = request.GetResponse();部分得到500错误。

正如我在Fiddler发布的几篇帖子中所建议的那样,我从中得到的回复看起来很好(虽然没有经验,但我可能会离开):

Request Count:   1
Bytes Sent:      85     (headers:85; body:0)
Bytes Received:  107        (headers:107; body:0)
Tunnel Sent:     1.740
Tunnel Received: 4.900

ACTUAL PERFORMANCE
--------------
ClientConnected:    11:03:28.895
ClientBeginRequest: 11:03:28.900
GotRequestHeaders:  11:03:28.900
ClientDoneRequest:  11:03:28.901
Determine Gateway:  0ms
DNS Lookup:         0ms
TCP/IP Connect: 12ms
HTTPS Handshake:    0ms
ServerConnected:    11:03:28.916
FiddlerBeginRequest:    11:03:28.916
ServerGotRequest:   11:03:28.916
ServerBeginResponse:    00:00:00.000
GotResponseHeaders: 00:00:00.000
ServerDoneResponse: 00:00:00.000
ClientBeginResponse:    11:03:28.917
ClientDoneResponse: 11:03:28.917

    Overall Elapsed:    0:00:00.017

RESPONSE BYTES (by Content-Type)
--------------
~headers~: 107

接下来我做的是尝试检索WebException,这就是它返回的内容: enter image description here

我还在SOAP UI中对它进行了测试,并且在修复Java错误之后似乎也有效(因此验证数据也是正确的)。

其他一些信息:

请记住,我之前从未使用过SOAP,所以如果问题显而易见,我会道歉:)

1 个答案:

答案 0 :(得分:4)

首先你说:

 request.ContentType = "text/xml;charset=\"utf-8\"";
  

翻译:嘿,我收到了一条消息,它是以XML和UTF8字节编码的形式。

然后你偷偷摸摸地做:

using (StreamWriter requestWriter = 
            new StreamWriter(webStream, System.Text.Encoding.ASCII))
  

翻译:传递一些黑暗的古代神秘(粘土)平板电脑,包含一些ASCII。

所以,坚持你的承诺并使用:

using (StreamWriter requestWriter = 
            new StreamWriter(webStream, System.Text.Encoding.UTF8))

顺便说一下,当您使用基于XML的内容(如soap或html)时,UTF8就是您的选择。

对于中止的异常,似乎您的连接是预先处理的。 您可能想尝试将try语句放在using块中:

HttpWebRequest request = (HttpWebRequest) WebRequest.Create("")

request.Method = "POST";
request.ContentType = "text/xml;charset=\"utf-8\"";
request.ContentLength = RequestingXMLData.Length;
using (Stream webStream = request.GetRequestStream())
using (StreamWriter requestWriter = new StreamWriter(webStream, System.Text.Encoding.UTF8))
{
   requestWriter.Write(RequestingXMLData);

   try
   {
       WebResponse webResponse = request.GetResponse();
       using (Stream webStream = webResponse.GetResponseStream())
       {
            if (webStream != null)
            {
                 using (StreamReader responseReader = new StreamReader(webStream))
                 {
                     response = responseReader.ReadToEnd();
                 }
             }
       }
   }
   catch (WebException e)
   {
        var resp = new StreamReader(e.Response.GetResponseStream()).ReadToEnd();

   }
}