Dot-Net

如何在使用 WCF(或 WSE 3)的 Web 服務客戶端應用程序的請求和響應中混合消息編碼類型(文本/MTOM)?

  • January 5, 2016

這是我的問題。我正在訪問一個 Web 服務(託管在基於 Java 的伺服器上),它只接受文本編碼的請求,但它返回 MTOM 響應。我發現如果我將 Web 服務設置為 RequireMtom,它會發送一個 Mtom 請求!不幸的是,伺服器在 Mtom 請求上阻塞並返回 500 錯誤。但是,如果我將其設置為文本消息編碼,則響應會正確返回,並帶有一個多部分 MIME (MTOM) 響應,該響應會導致 Microsoft Web 服務 API 出錯(下面的範例錯誤)。它期待一個文本編碼的響應,因為請求是文本編碼的。我只想在響應中使用 RequireMtom。有人能幫我一下嗎?

正如您在下面的錯誤中看到的(發生在標準 Web 服務 API、WCF 或 WSE3 中),當我發送帶有文本編碼的請求時,響應會正確返回,其中包含多部分/相關響應中的所有數據,但是.net 框架窒息!

WSE 的錯誤消息:

Client found response content type of 'multipart/related; type="text/xml"; start="<1AE0B46A85B0186B5D136D12E1EE286E>";  boundary="----=_Part_209564_1891070135.1226526701833"', but expected 'text/xml'.
The request failed with the error message:

at System.Web.Services.Protocols.SoapHttpClientProtocol.ReadResponse(SoapClientMessage message, WebResponse response, Stream responseStream, Boolean asyncCall)
  at System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters)
  at TestWseService.AdesaJasperWse.ManagementServiceService.runReport(String requestXmlString) in C:\Documents and Settings\xxx\My Documents\Visual Studio 2005\Projects\TestWseService\Web References\AdesaJasperWse\Reference.cs:line 229
  at TestWseService.Form1.buttonRunService_Click(Object sender, EventArgs e) in C:\Documents and Settings\xxx\My Documents\Visual Studio 2005\Projects\TestWseService\Form1.cs:line 42

WCF 的錯誤消息

The content type multipart/related; type="text/xml"; start="<30ED8FE3004CDA67723CC7164A6CFEEC>";    boundary="----=_Part_209545_389093169.1226526546805" of the response message does not match the content type of the binding (text/xml; charset=utf-8). If using a custom encoder, be sure that the IsContentTypeSupported method is implemented properly. The first 1024 bytes of the response were: 

Server stack trace: 
  at System.ServiceModel.Channels.HttpChannelUtilities.ValidateRequestReplyResponse(HttpWebRequest request, HttpWebResponse response, HttpChannelFactory factory, WebException responseException)
  at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
  at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)
  at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout)
  at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
  at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs)
  at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
  at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

請求(帶有文本消息編碼):

POST /jasperserver-pro/services/repository HTTP/1.1
Content-Type: text/xml; charset=utf-8
VsDebuggerCausalityData: uIDPo7V2+runH+xGudbec7ueUU8AAAAA7H9vL3stlkCBofMgLa5DWkQOHHpBdy1Ek6P6nXx7FpsACQAA
SOAPAction: ""
Authorization: Basic amFzcGVyYWRtaW46akBzcDNyQGRtJW4=
Host: reports.dev.xxx.com
Content-Length: 789
Expect: 100-continue

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><q1:runReport xmlns:q1="http://axis2.ws.jasperserver.jaspersoft.com"><requestXmlString xsi:type="xsd:string"><request operationName="runReport" locale="en">
<argument name="RUN_OUTPUT_FORMAT">HTML</argument>
<resourceDescriptor name="" wsType="" uriString="/BusinessIntelligence/MOS/Reports/dotnettest" isNew="false">
<label>null</label>
<parameter name="testparam">1</parameter>
</resourceDescriptor>
</request></requestXmlString></q1:runReport></s:Body></s:Envelope>

響應(帶有文本消息編碼):

HTTP/1.1 200 OK
Date: Wed, 12 Nov 2008 21:49:04 GMT
Server: IBM_HTTP_Server
Surrogate-Control: no-store
Set-Cookie: JSESSIONID=0000z5pH1xEMyulueASctjru2qe:13kftunf6; Path=/
Expires: Thu, 01 Dec 1994 16:00:00 GMT
Cache-Control: no-cache="set-cookie, set-cookie2"
Content-Length: 2580
Content-Type: multipart/related; type="text/xml"; start="<30ED8FE3004CDA67723CC7164A6CFEEC>";   boundary="----=_Part_209545_389093169.1226526546805"
Content-Language: en-US


------=_Part_209545_389093169.1226526546805
Content-Type: text/xml; charset=UTF-8
Content-Transfer-Encoding: binary
Content-Id: <30ED8FE3004CDA67723CC7164A6CFEEC>

<?xml version="1.0" encoding="utf-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><ns1:runReportResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://axis2.ws.jasperserver.jaspersoft.com"><runReportReturn xsi:type="xsd:string"><?xml version="1.0" encoding="UTF-8"?>
<operationResult version="2.0.1">
   <returnCode><![CDATA[0]]></returnCode>
</operationResult>
</runReportReturn></ns1:runReportResponse></soapenv:Body></soapenv:Envelope>
------=_Part_209545_389093169.1226526546805
Content-Type: text/html
Content-Transfer-Encoding: binary
Content-Id: <report>

<html>
<head>
 <title></title>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
 <style type="text/css">
   a {text-decoration: none}
 </style>
</head>
<body text="#000000" link="#000000" alink="#000000" vlink="#000000">
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr><td width="50%"> </td><td align="center">

<a name="JR_PAGE_ANCHOR_0_1"/>
<table style="width: 595px" cellpadding="0" cellspacing="0" border="0" bgcolor="white">
<tr>
 <td><img alt="" src="images/px" style="width: 35px; height: 1px;"/></td>
 <td><img alt="" src="images/px" style="width: 189px; height: 1px;"/></td>
 <td><img alt="" src="images/px" style="width: 253px; height: 1px;"/></td>
 <td><img alt="" src="images/px" style="width: 118px; height: 1px;"/></td>
</tr>
<tr valign="top">
 <td colspan="4"><img alt="" src="images/px" style="width: 595px; height: 20px;"/></td>
</tr>
<tr valign="top">
 <td><img alt="" src="images/px" style="width: 35px; height: 30px;"/></td>
 <td valign="middle"><span style="font-family: Arial; font-size: 12.0px; font-weight: bold;">The value of the parameter is:</span></td>
 <td valign="middle"><span style="font-family: Arial; background-color: #FFFFFF; font-size: 12.0px; font-weight: bold;">1</span></td>
 <td><img alt="" src="images/px" style="width: 118px; height: 30px;"/></td>
</tr>
<tr valign="top">
 <td colspan="4"><img alt="" src="images/px" style="width: 595px; height: 20px;"/></td>
</tr>
</table>

</td><td width="50%"> </td></tr>
</table>
</body>
</html>

------=_Part_209545_389093169.1226526546805--

請求(使用 Mtom 消息編碼):

POST /jasperserver-pro/services/repository HTTP/1.1
MIME-Version: 1.0
Content-Type: multipart/related; type="application/xop+xml";start="<http://tempuri.org/0>";boundary="uuid:fafcdca7-94f7-4884-a8d4-5c6d50dbe8ef+id=1";start-info="text/xml"
VsDebuggerCausalityData: uIDPo+cN2kKX2odFuUVaER0j60gAAAAAmfYaGH7Ow0WQOcwhebh5pqmDl29omcVOtwVGa10IWewACQAA
SOAPAction: ""
Authorization: Basic amFzcGVyYWRtaW46akBzcDNyQGRtJW4=
Host: reports.dev.xxx.com
Content-Length: 1031
Expect: 100-continue


--uuid:fafcdca7-94f7-4884-a8d4-5c6d50dbe8ef+id=1
Content-ID: <http://tempuri.org/0>
Content-Transfer-Encoding: 8bit
Content-Type: application/xop+xml;charset=utf-8;type="text/xml"

&lt;s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"&gt;&lt;s:Body s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;&lt;q1:runReport xmlns:q1="http://axis2.ws.jasperserver.jaspersoft.com"&gt;&lt;requestXmlString xsi:type="xsd:string"&gt;&lt;request operationName="runReport" locale="en"&gt;&#xD;
&lt;argument name="RUN_OUTPUT_FORMAT"&gt;HTML&lt;/argument&gt;&#xD;
&lt;resourceDescriptor name="" wsType="" uriString="/BusinessIntelligence/MOS/Reports/dotnettest" isNew="false"&gt;&#xD;
&lt;label&gt;null&lt;/label&gt;&#xD;
&lt;parameter name="testparam"&gt;1&lt;/parameter&gt;&#xD;
&lt;/resourceDescriptor&gt;&#xD;
&lt;/request&gt;&lt;/requestXmlString&gt;&lt;/q1:runReport&gt;&lt;/s:Body&gt;&lt;/s:Envelope&gt;
--uuid:fafcdca7-94f7-4884-a8d4-5c6d50dbe8ef+id=1--

響應(使用 Mtom 消息編碼):

HTTP/1.1 500 Internal Server Error
Date: Wed, 12 Nov 2008 21:47:42 GMT
Server: IBM_HTTP_Server
Surrogate-Control: no-store
$WSEP: 
Set-Cookie: JSESSIONID=0000_iMrdp-TnK9FG3jZFzjx_hA:13kftunf6; Path=/
Expires: Thu, 01 Dec 1994 16:00:00 GMT
Cache-Control: no-cache="set-cookie, set-cookie2"
Content-Length: 12
Connection: close
Content-Type: text/html;charset=UTF-8
Content-Language: en-US

Error 500: 

這是一個支持微軟不支持混合編碼理論的連結:

WSE 3.0:MTOM 請求必須響應 MTOM?

超高!

是的,您可以使用 WCF 發送簡訊並獲得 mtom 回复(反之亦然)。

看…

http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/480f1bc4-1fc4-40e9-a2ed-efcf3009d6ef

我對此進行了一些研究,壞消息是從 WCF 的角度來看,請求和響應必須使用相同的編碼。所以,是的,上面的答案是非常正確的。您必須與服務提供商組織以在請求和響應中啟用 MTOM。據我所知,除了更改 SOAP mime-type 之外,MTOM 請求不會對他的應用程序產生任何影響。

引用自:https://stackoverflow.com/questions/284149