C#解析来自Web服务的MTOM响应

时间:2017-05-23 15:25:53

标签: c# xml soap mtom

供应商Web服务的响应类型为MTOM):

--uuid: 5251c679-2917-1111-bbbf-44a5963ee0ac
Content-Type: application/xop+xml;Encoding=UTF-8;Type="text/XML"
Content-Transfer-Encoding: binary code
Content-ID: <root.message@cxf.apache.org>

<soap: Envelope xmlns: soap = "http://schemas.xmlsoap.org/soap/envelope/">
/ * Here the data xml * /
</ soap: Envelope>
--uuid: 5251c679-2917-1111-bbbf-44a5963ee0ac
Content Type: application / octet stream
Content-Transfer-Encoding: binary code
Content-ID: <fb67ac70-5a7e-47f5-b805-7ec4b14e1857-524 @ urn: // XXX>

/ * Here the binary data * /
--uuid: 5251c679-2917-1111-bbbf-44a5963ee0ac--

如何解析来自网络服务的响应并获取xml?

以前,该服务返回了通常的&#34; text / xml&#34;。

以前处理过网络服务响应数据的代码:

HttpWebRequest webRequest = (HttpWebRequest) WebRequest.Create (RequestURI);
WebRequest.ContentType = "text/xml;charset=\"utf-8\"";
WebRequest.Method = "POST";
WebRequest.Timeout = 30000;
WebRequest.Headers.Add("SOAPAction", SOAPAction);
using (Stream stream = webRequest.GetRequestStream())
{
    Request.Save(stream);
}
try
{
    IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null);
    asyncResult.AsyncWaitHandle.WaitOne();
    MemoryStream MStream = new MemoryStream();

    using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult))
    {
        ResponseStream = webResponse.GetResponseStream();
        ResponseStream.CopyTo(MStream);
    }

    XmlDictionaryReader xmlReader = XmlDictionaryReader.CreateMtomReader(MStream, Encoding.UTF8, XmlDictionaryReaderQuotas.Max);

    using (var sr = new StreamReader(webResponse.GetResponseStream()))
    {
        return sr.ReadToEnd();
    }
}
catch (WebException webex)
{
    String data = String.Empty;
    if (webex.Response != null)
    {
        StreamReader r = new StreamReader(webex.Response.GetResponseStream());
        data = r.ReadToEnd();
        r.Close();
    }
    Controllers.Logger.AddToLog(Helper.LogStatuses.ERROR, null, "WebServicesClient-SendMessage-catch WebException: data.ToString()");
    return data.ToString();
}

现在错误FormatException&#34;错误地形成标题&#34;在此字符串XmlDictionaryReader xmlReader = XmlDictionaryReader.CreateMtomReader(MStream, Encoding.UTF8, XmlDictionaryReaderQuotas.Max);

0 个答案:

没有答案