在WCF中强制使用HttpClient关闭连接 [英] Force connection to close with HttpClient in WCF

查看:70
本文介绍了在WCF中强制使用HttpClient关闭连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在将"Rest工具箱"中的HttpClient类用于WCF(http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=24644 )来连接我们创建的Rest服务器.

We're using the HttpClient class from "Rest toolkit" for WCF ( http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=24644 ) to inteface a Rest-server we've created.

服务器当前总是关闭连接,而不管连接"标头是什么(正在开发中,所以现在可以).

The server currently always close the connection, regardless of the "connection" header (it is in developement, so that is ok for now).

如何告诉HttpClient实例始终关闭连接(或接受服务器关闭连接)?我尝试添加连接:关闭"标头,但出现例外(连接"不是允许的标头).我还尝试设置DefaultHeaders.Connection.Close = true,但这似乎没有任何区别.POST完成后,我仍然可以看到与netstat的连接.

How can I tell the HttpClient instance to always close the connection (or accept that the server closes it)? I tried adding the " Connection: close" header, but resulted in an exeption ("connection" was not an allowed header). I also tried to set DefaultHeaders.Connection.Close = true, but this didn't seem to make any difference. I can still see the connection with netstat after the POST is done.

( body uri 是字符串)

HttpClient client = new HttpClient();
client.DefaultHeaders.Connection = new Connection();
client.DefaultHeaders.Connection.Close = true;
HttpContent content = HttpContent.Create(body);
HttpResponseMessage res = client.Post(new Uri(uri), content);

这里的问题是,下一次我们进行POST时,呼叫将阻塞.我们认为这是由于这样的事实,即该连接由客户端保持活动状态,而服务器不支持此连接.

The problem here is that the next time we do a POST, the call just blocks. We think this is due to the fact that the connection is kept alive by the client, and this is not supported by the server.

推荐答案

我写了大多数HttpClient代码;这里有一些想法:

I wrote most of the HttpClient code; here are some thoughts:

您无需告诉HttpClient接受服务器关闭连接-它应该会自动工作-服务器可以始终关闭连接,无论客户端是否包含"Connection:close"标头

You don't need to tell HttpClient to accept the server closing the connection -- it should just work automatically -- the server can always close the connection whether or not the client includes the "Connection: close" header

使用response.Dispose()-基础连接可能无法启动新连接/发送字节,直到您完成读取待处理的字节为止

Use response.Dispose() -- the underlying connection may not be able to initiate a new connection / send bytes until you're done reading the bytes that are pending

client.DefaultHeaders.Add("Connection","close");应该可以正常工作-如果遇到异常,请在Codeplex网站上打开一个问题

client.DefaultHeaders.Add("Connection", "close"); should work -- if you get an exception, please open an issue on the codeplex site

您可以检查response.Request.Header以查看线路上发生了什么

You can inspect response.Request.Headers to see what's going out on the wire

您可以跳过Post(new Uri(x))中的"new Uri()"-传递字符串将为您调用正确的构造函数(new Uri(x,UriKind.RelativeOrAbsolute))

You can skip the "new Uri()" in Post(new Uri(x)) -- passing a string will call the right constructor (new Uri(x, UriKind.RelativeOrAbsolute)) for you

默认情况下,超时与HttpWebRequest相同-您可能希望通过client.TransportSettings.ReadWriteTimeout/client.TransportSettings.ConnectionTimeout来将其关闭,以区分永久阻止和超时

By default, the timeouts are the same as HttpWebRequest -- you might want to turn them down via client.TransportSettings.ReadWriteTimeout / client.TransportSettings.ConnectionTimeout to distinguish between blocking forever and timing out

这篇关于在WCF中强制使用HttpClient关闭连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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