C#得到的WebClient摆脱连接头的 [英] C# get rid of Connection header in WebClient

查看:159
本文介绍了C#得到的WebClient摆脱连接头的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是C#中使用WebClient的()。



我是测试出来被送到什么头,我注意到下面的头会自动添加。

 连接:保持活动

有什么办法去除呢?


<类=h2_linDIV>解决方案

我已经遇到了同样的问题,今天上午。

$ B:关于飞碟双向的提示之后,就可以通过传递的HttpWebRequest Web客户端通过继承它来实现
$ b

 类MyWebClient:Web客户端
{
保护覆盖的WebRequest GetWebRequest(URI地址)
{
的WebRequest请求= base.GetWebRequest(地址);
如果(请求的HttpWebRequest)
{
(要求为HttpWebRequest的).KeepAlive = FALSE;
}
返回请求;
}
}

现在发送标题将包括连接:关闭


I'm using the C# using the WebClient().

I was testing out what headers are sent, and I noticed that the following header is automatically added.

Connection : Keep-Alive

Is there any way to remove this?

解决方案

I had ran into the same issue this morning. Following on Skeet's hint, it can be achieved by passing HttpWebRequest to WebClient by inheriting it:

class MyWebClient : WebClient
{
    protected override WebRequest GetWebRequest(Uri address)
    {
        WebRequest request = base.GetWebRequest(address);
        if (request is HttpWebRequest)
        {
            (request as HttpWebRequest).KeepAlive = false;
        }
        return request;
    }
}

Now sent headers will include Connection : close

这篇关于C#得到的WebClient摆脱连接头的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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