无法从httpwerequest类创建的Http请求中删除Connection标头

时间:2016-09-15 10:01:24

标签: c# file-upload http-headers httpwebrequest

我想从我的分段上传请求中删除Connection标头。但即使我将HttpWebrequest.Connection属性设置为null,它也没有被删除。我也在运行时检查过,值为null。我尝试更改值在运行时到其他一些字符串,但它甚至不允许我这样做。我在通过Httpwerequest上传大文件时遇到超时问题,但在使用webclient类时我不会遇到这样的问题。 fiddler我发现webclient和httpwebrequest生成的请求之间的区别在于webclient没有指定Connection头,但httpwebrequest类却没有。我想使用httpwebrequest,因为它的速度是webclient类的两倍。所以,如何我摆脱连接头?这是我正在使用的代码:

HttpWebRequest request = WebRequest.Create(postUrl) as HttpWebRequest;

request.Method = "POST";
request.ContentType = contentType;
request.CookieContainer = new CookieContainer();
request.ContentLength = formData.Length;
if (headers != null)
{
    foreach (KeyValuePair<string, string> header in headers)
    {
        request.Headers[header.Key] = header.Value; 
    }
}
request.KeepAlive = false;
request.Connection = null;

using (Stream requestStream = request.GetRequestStream())
{
    requestStream.Write(formData, 0, formData.Length);
    requestStream.Close();
}
return request.GetResponse() as HttpWebResponse;

0 个答案:

没有答案