出站消息上的WCF内容长度HTTP标头 [英] WCF Content-Length HTTP header on outbound message
问题描述
我处境艰难,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标头的好方法。我可以使用IClientMessageInspector为请求添加任意标头(即X-Dan-Lynn-Header),如这样的博客,但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)生成的示例请求:
Sample request generated by WCF (and the preceding 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:chunked。由于网络服务的回复非常大,我们需要流式传输,因此我可以使用:
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"
好:
Good:
transferMode="StreamedResponse"
更改绑定到此解决了问题:
Changing the binding to this solved the problem:
<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内容长度HTTP标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!