出站消息上的 WCF Content-Length HTTP 标头 [英] WCF Content-Length HTTP header on outbound message

查看:24
本文介绍了出站消息上的 WCF Content-Length HTTP 标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的处境很艰难,托管在 IBM HTTP Server (IHS) 上的 Java Web 服务端点需要 Content-Length 标头,尽管它应该符合 HTTP/1.1.如果我发送标题,一切正常.如果我不使用它,我会收到 500 错误响应,通知我我的 POST 实体正文为空(即使它不是).

I'm in a tough situation in which a Java web service endpoint hosted on an IBM HTTP Server (IHS) requires a Content-Length header, although it supposedly conforms to HTTP/1.1. If I send the header, everything works. If I leave it off, I get a 500 error response informing me that my POST entity body was empty (even though it was not).

我们为这些服务(由第三方开发)在 WCF 客户端上投入了大量时间,但我似乎找不到将 Content-Length 标头附加到请求的好方法.如 这样的博客文章,但 WCF 似乎忽略了 Content-Length 标头.

We've invested significant time into our WCF client for these services (developed by a third party) and I can't seem to find a good way to append a Content-Length header to the request. I am able to add arbitrary headers (i.e. X-Dan-Lynn-Header) to the request using an IClientMessageInspector as described in blog posts like this, but WCF seems to ignore a Content-Length header.

我的选择是:

a) 弄清楚如何强制 WCF 将 Content-Length 标头附加到 HTTP POST 请求,或者,

a) figure out how to force WCF to append a Content-Length header to the HTTP POST request or,

b) 找到或编写一个极其简单但透明的 HTTP 代理,用 Content-Length 标头装饰请求.

b) find or write an extremely simple-yet-transparent HTTP proxy that decorates the request with a Content-Length header.

谢谢!


public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
    var buffer = request.CreateBufferedCopy(Int32.MaxValue);
    var tempRequest = buffer.CreateMessage();


    HttpRequestMessageProperty httpRequest = GetHttpRequestProp(tempRequest);
    if (httpRequest != null)
    {
        if (string.IsNullOrEmpty(httpRequest.Headers[HttpRequestHeader.ContentLength]))
        {
            httpRequest.Headers.Add(HttpRequestHeader.ContentLength, GetMessageLength(buffer).ToString());
            httpRequest.Headers.Add("X-Dan-Lynn-Header", "abcdefghijk");
        }

    }

    request = tempRequest;
    request.Properties[HttpRequestMessageProperty.Name] = httpRequest;

    return null;
}

WCF(和前面的 IClientMessageInspector)生成的示例请求:

POST /path/to/service HTTP/1.1
Content-Type: text/xml; charset=utf-8
X-Dan-Lynn-Header: abcdefghijk
SOAPAction: "http://tempuri.org/path/to/service/action"
Host: service.host.tld
Transfer-Encoding: chunked
Connection: Keep-Alive


<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        .......body removed for clarity......

    </s:Body>
</s:Envelope>

推荐答案

想通了.将绑定设置为使用 transferMode="Streamed" 会导致 Transfer-Encoding: 分块.由于来自 Web 服务的响应非常大,我们需要流式传输,因此我能够使用:

Figured it out. Setting the binding to use transferMode="Streamed" was causing a Transfer-Encoding: chunked. We needed streamed transfers due to very large responses from the web service, so I was able to go with:

transferMode="Streamed"

好:

transferMode="StreamedResponse"

将绑定更改为 this 解决了问题:

<basicHttpBinding>
    <binding name="MyBinding" closeTimeout="00:30:00" openTimeout="00:30:00"
      receiveTimeout="00:30:00" sendTimeout="00:30:00" allowCookies="false"
      bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
      maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="16777216"
      messageEncoding="Text" textEncoding="utf-8" transferMode="StreamedResponse"
      useDefaultWebProxy="false">
      <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="65536"
        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
      <security mode="None" />
    </binding>
  </basicHttpBinding>

这篇关于出站消息上的 WCF Content-Length HTTP 标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆