【问题标题】:posting to XML soap service and having a hard time getting the server response发布到 XML soap 服务并且很难获得服务器响应
【发布时间】:2016-02-03 17:36:57
【问题描述】:

当代码到达使用(WebResponse webResponse = webRequest.EndGetResponse(asyncResult)) 的行时,会引发 500 html 错误响应。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

public partial class RawService : System.Web.UI.Page
{
    static string _url = "https://iserv.intecon.co.za/allpsws_test/allps.asmx";
    static string _action = "https://iserv.intecon.co.za/allpsws_test/allps.asmx";
    static string _inputPath = @"C:\intercon\input.xml";
    static string _outputPath = @"C:\intercon\output.xml";
    static string _soapEnvelope =
@"<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></soap:Body></soap:Envelope>";

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    private static HttpWebRequest CreateWebRequest(string url, string action)
    {
        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
        webRequest.Headers.Add("SOAPAction", action);
        webRequest.ContentType = "text/xml;charset=\"utf-8\"";
        webRequest.Accept = "text/xml";
        webRequest.Method = "POST";
        return webRequest;
    }

    private static XmlDocument CreateSoapEnvelope(string content)
    {
        StringBuilder sb = new StringBuilder(_soapEnvelope);
        sb.Insert(sb.ToString().IndexOf("</soap:Body>"), content);

        // create an empty soap envelope
        XmlDocument soapEnvelopeXml = new XmlDocument();
        soapEnvelopeXml.LoadXml(sb.ToString());

        return soapEnvelopeXml;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string content = File.ReadAllText(_inputPath);
        XmlDocument soapEnvelopeXml = CreateSoapEnvelope(content);
        HttpWebRequest webRequest = CreateWebRequest(_url, _action);
        InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);

        // begin async call to web request.
        IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null);

        // suspend this thread until call is complete. You might want to
        // do something usefull here like update your UI.
        asyncResult.AsyncWaitHandle.WaitOne();

        // get the response from the completed web request.
        string soapResult;
        using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult))
        using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
        {
            soapResult = rd.ReadToEnd();
        }
        File.WriteAllText(_outputPath, (soapResult));
    }

    private static void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest)
    {
        using (Stream stream = webRequest.GetRequestStream())
        {
            soapEnvelopeXml.Save(stream);
        }
    }

}

我需要做的就是获取服务响应,以便我能够使用这些方法

【问题讨论】:

  • 为什么url和action的值一样?那是错字吗? input.xml 中有什么? 500是服务器抛出的,所以通过查看客户端代码很难说什么?你有那个网络服务的代码吗?也许您可以调试它或添加一些日志记录或检查现有的日志
  • 输入没有任何内容..响应将存储在该路径和文件夹中,至于 url 和操作,我没有任何操作可以放在那里,当我使用相同的路径时它似乎会通过对于 url 和操作。 Web 服务没有 WSDL 文档,我所拥有的只是要处理的请求和响应的示例

标签: c# asp.net xml web-services soap


【解决方案1】:

您的 SOAP 操作似乎是错误的。如果您查看此处的架构 (https://iserv.intecon.co.za/allpsws_test/allps.asmx?op=Call),您可以看到它正在等待

SOAPAction: "http://intecon.co.za/webservices/allps/Call"

与 Fiddler(或类似的东西)一起玩,并尝试确保您可以让调用在您的代码之外工作。

使用此标头:

Host: iserv.intecon.co.za
Content-Type: text/xml; charset=utf-8
SOAPAction: "http://intecon.co.za/webservices/allps/Call"
Content-Length: 364

还有这个身体:

<?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>
    <Call xmlns="http://intecon.co.za/webservices/allps/">
      <xmlrequest></xmlrequest>
    </Call>
  </soap:Body>
</soap:Envelope>

我收到 200 OK 响应 - 尽管看起来我正在调用的系统存在问题,可能是因为我不知道我应该传递的 XMLRequest 应该是什么样子。

编辑:将一些代码放在一起作为示例:

static void Main( string[] args )
{
    HttpWebRequest request = ( HttpWebRequest )WebRequest.Create( "https://iserv.intecon.co.za/allpsws_test/allps.asmx" );

    request.Headers.Add( "SOAPAction", "http://intecon.co.za/webservices/allps/Call" );
    request.ContentType = "text/xml; charset=utf-8";
    request.Accept = "text/xml";
    request.Method = "POST";

    XmlDocument soapRequest = CreateEnvelope();

    using ( Stream stream = request.GetRequestStream() )
    {
        soapRequest.Save( stream );
    }

    HttpWebResponse webResponse = ( HttpWebResponse )request.GetResponse();

    StreamReader reader = new StreamReader( webResponse.GetResponseStream() );

    string response = reader.ReadToEnd();

    Console.WriteLine( response );
}

private static XmlDocument CreateEnvelope()
{
    var xmlDocument = new XmlDocument();

    xmlDocument.LoadXml( @"<?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>
                            <Call xmlns=""http://intecon.co.za/webservices/allps/"">
                              <xmlrequest></xmlrequest>
                            </Call>
                          </soap:Body>
                        </soap:Envelope>" );

    return xmlDocument;
}

【讨论】:

  • 如果你不介意的话,你能不能告诉我你是如何编码出来的......就像分享整个代码和结构一样,请求应该是这样的:
  • testtsetpc_nameallps-wsSomeSoft ThisProd1.0.0.1。这是对整个会话进行身份验证并获取允许我发送信息的方法。
  • intecon.co.za/webservices/allps/…> intellicellWS*******nameSomeCompanySomeProduct1.0.0.1 就是这样输入 xml 有什么
  • 我没有把它编码出来。我使用 Fiddler 使用我发布的这些值发出原始 HTTP 请求,以查看是否可以从服务器获得响应。让我看看我是否可以为它编写一些代码。
猜你喜欢
  • 1970-01-01
  • 2017-03-19
  • 1970-01-01
  • 2014-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多