简体   繁体   English

为 WCF 消息设置 HTTP 内容类型

[英]Setting HTTP content-type for WCF message

I'm using an external SOAP service, the european National Medicines Verification Systems, over which I have no control.我正在使用外部 SOAP 服务,即欧洲国家药品验证系统,我无法控制它。 My application uses a WCF client automatically generated from the supplied WSDL.我的应用程序使用从提供的 WSDL 自动生成的 WCF 客户端。 The client uses System.ServiceModel.Channels.Message messages.客户端使用 System.ServiceModel.Channels.Message 消息。

The SOAP service uses the same message structure, an "I7RequestType", for 2 messages, the G482LoadTermsAndConditions and the G487LoadDataPrivacyPolicies, and does not "understand" any Action header. SOAP 服务使用相同的消息结构,即“I7RequestType”,用于 2 个消息,即 G482LoadTermsAndConditions 和 G487LoadDataPrivacyPolicies,并且不“理解”任何 Action 标头。

This is my request message, with the ignored Action header :这是我的请求消息,忽略了 Action 标头:

<?xml version="1.0" encoding="UTF-8"?>
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">
   <s:Header>
      <a:Action s:mustUnderstand="0">ns:G482RequestMessage</a:Action>
   </s:Header>
   <s:Body xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <I7RequestType xmlns="urn:wsdltypes.nmvs.eu:v4.0">
         <Header xmlns="urn:types.nmvs.eu:v4.0">
            <Auth>
               <ClientLoginId>xxxxx</ClientLoginId>
               <UserId>xxxxx</UserId>
               <Password>xxxxx</Password>
            </Auth>
            <UserSoftware xmlns:d5p1="urn:types.nmvs.eu:v4.0" d5p1:name="xxxxx" d5p1:supplier="xxxxx" d5p1:version="2.0" />
            <Transaction>
               <ClientTrxId>xxxxxxxxxxxxxxx</ClientTrxId>
               <Language>fra</Language>
            </Transaction>
         </Header>
      </I7RequestType>
   </s:Body>
</s:Envelope>

The Header Action is ignored by the server, which returns a fault if mustUnderstand is set to 1. Header Action 被服务器忽略,如果mustUnderstand设置为 1,它会返回一个错误。

Apparently I should be using the HTTP content-type to permit the server to differentiate the two messages.显然我应该使用 HTTP 内容类型来允许服务器区分这两条消息。 This is the example I have been given:这是我得到的例子:

    Content-Type : application/soap+xml;charset=utf-8;method=SUPPORT-I7RequestType;action="urn:G482LoadTermsAndConditions"; 

My custom MessageInspector can manipulate the message content in BeforeSendRequest(), but I cannot find any way of adding/modifying the encapsulating HTTP message's headers.我的自定义 MessageInspector 可以操作 BeforeSendRequest() 中的消息内容,但是我找不到任何添加/修改封装 HTTP 消息标头的方法。

Does anybody know any way of modifying the HTTP headers of an outbound System.ServiceModel.Channels.Message ?有人知道修改出站 System.ServiceModel.Channels.Message 的 HTTP 标头的任何方法吗?

Thanks in advance.提前致谢。

After much searching the problem seems to be due to the generated WCF client not respecting the WSDL and the SOAP 1.2 specification.经过大量搜索,问题似乎是由于生成的 WCF 客户端不遵守 WSDL 和 SOAP 1.2 规范。

This answer WCF Service with wsHttpBinding - Manipulating HTTP request headers gave me all that I needed to create a workaround.这个答案WCF Service with wsHttpBinding - Manipulating HTTP request headers为我提供了创建解决方法所需的一切。

This is the hard-coded proof-of-concept code, based very heavily on Dejan Janjušević's reply, that I added in the function BeforeSendRequest() of my CustomMessageInspector (implements IEndpointBehavior and IClientMessageInspector).这是硬编码的概念验证代码,主要基于 Dejan Janjušević 的回复,我在 CustomMessageInspector(实现 IEndpointBehavior 和 IClientMessageInspector)的函数 BeforeSendRequest() 中添加了该代码。

        // Making sure we have a HttpRequestMessageProperty
        HttpRequestMessageProperty httpRequestMessageProperty;
        if( request.Properties.ContainsKey( HttpRequestMessageProperty.Name ) )
        {
            httpRequestMessageProperty = request.Properties[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty;
            if( httpRequestMessageProperty == null )
            {
                httpRequestMessageProperty = new HttpRequestMessageProperty();
                request.Properties.Add( HttpRequestMessageProperty.Name, httpRequestMessageProperty );
            }
        }
        else
        {
            httpRequestMessageProperty = new HttpRequestMessageProperty();
            request.Properties.Add( HttpRequestMessageProperty.Name, httpRequestMessageProperty );
        }

        Console.WriteLine("original Request:");
        Console.WriteLine("{0}\r\n", request);
        request.Headers.Clear();

        httpRequestMessageProperty.Headers.Add( "Content-Type", "application/soap+xml;charset=utf-8;method=SUPPORT-I7RequestType;action=\"urn:G482LoadTermsAndConditions\"" );

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

相关问题 WCF服务返回HTTP 400,但仅当Content-Type设置为“text / xml”或“text / json”时 - WCF service returns HTTP 400, but only when Content-Type is set to “text/xml” or “text/json” 当Post-Type未在Postman中设置为application / json时,WCF返回HTTP 400,否则它可以正常工作。 - WCF returns HTTP 400 when Content-Type is not Set to application/json in Postman, else it works fine. 如何将Content-Length,Content-Type和Last-Modified添加到HTTP响应消息头中 - How to add the Content-Length,Content-Type and Last-Modified to the HTTP Response Message Header c#设置uploadFile内容类型 - c# setting uploadFile content-Type 设置Web API 2响应的内容类型 - Setting content-type for Web API 2 response HttpClient设置与内容类型的边界 - HttpClient setting boundary with content-type 使用Amazon API for .NET设置内容编码和内容类型 - Setting content-encoding and content-type with Amazon API for .NET WCF服务不喜欢我的内容类型 - WCF Service Doesn't Like My Content-Type 使用“内容类型”编码通过WCF上传文件 - Uploading files over WCF using “content-type” encoding WCF WebInvoke可以接受内容类型:text / plain? - WCF WebInvoke which can accept content-type: text/plain?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM