在WinRT的HttpClient类中使用Keep-Alive连接吗? [英] Using a Keep-Alive connection in WinRT's HttpClient class?

查看:58
本文介绍了在WinRT的HttpClient类中使用Keep-Alive连接吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

打开与我们服务器的连接时,我们的WinRT应用程序的运行速度非常慢。请求大约需要500毫秒才能运行。



调试时,我们注意到当Fiddler处于活动状态时,请求要快得多-每个请求大约100ms。后来进行了一些搜索,我们了解到这是因为Fiddler在代理呼叫时正在使用Keep-Alive连接,这使我们的代理呼叫要快得多。 。


  1. 我们将UseProxy设置为false,并观察到请求恢复缓慢。

  2. 我们关闭了Fiddler的重用连接选项,并观察到请求恢复缓慢。

我们尝试通过以下方式启用保持活动状态: Connection标头(.Connection.Add( Keep-Alive)),但这似乎没有任何作用-实际上,标头似乎被.NET组件公然忽略,并且未在请求中发送(



有人知道如何在Windows 8,WinRT,HttpClient类中的请求上设置保持活动状态吗?

解决方案

以下内容为我设置了正确的标头,以启用保持活动状态(客户端是HttpClient)

  client.DefaultRequestHeaders.Connection.Clear(); 
client.DefaultRequestHeaders.ConnectionClose = false;
// HTTP / 1.1中不需要下一行
client.DefaultRequestHeaders.Connection.Add( Keep-Alive);

如果要关闭保持活动状态,请使用

  client.DefaultRequestHeaders.Connection.Clear(); 
client.DefaultRequestHeaders.ConnectionClose = true;


Our WinRT app is incredibly slow when opening connections to our servers. Requests take ~500ms to run. This is blocking some of our scenarios.

When debugging, we noticed that when Fiddler is active, the requests are much faster - ~100ms per request. Some searches later we understood that was because Fiddler was using Keep-Alive connections when proxying calls, which makes our proxied calls much faster.

We double-checked this in two ways.

  1. We set UseProxy to false and observed that the request went back to being slow.
  2. We turned off Fiddler's "reuse connections" option and observed that the requests went back to being slow.

We tried enabling keep-alive through the Connection header (.Connection.Add("Keep-Alive")) but this does not seem to have any effect - in fact, the header seems to be blatantly ignored by the .NET component and is not being sent on the request (again, by inspecting thru Fiddler).

Does anyone know how to set keep-alive on requests in Windows 8, WinRT, HttpClient class?

解决方案

The following sets the correct headers to turn on keep-alive for me (client is an HttpClient)

client.DefaultRequestHeaders.Connection.Clear();
client.DefaultRequestHeaders.ConnectionClose = false;
// The next line isn't needed in HTTP/1.1
client.DefaultRequestHeaders.Connection.Add("Keep-Alive");

If you want to turn keep-alive off, use

client.DefaultRequestHeaders.Connection.Clear();
client.DefaultRequestHeaders.ConnectionClose = true;

这篇关于在WinRT的HttpClient类中使用Keep-Alive连接吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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