如何正确发送保持连接头在C#? [英] How to send KeepAlive header correctly in c#?

查看:181
本文介绍了如何正确发送保持连接头在C#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要发送这样使用HttpWebRequest的一个请求:

I need to send a request like this using HttpWebRequest:

POST https://sap.site.com.mx/sap/bw/BEx?SAP-LANGUAGE=ES&PAGENO=1&CMD=PROCESS_VARIABLES&REQUEST_NO=0&CMD=PROCESS_VARIABLES&SUBCMD=VAR_SUBMIT&VARID= HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:10.0.2) Gecko/20100101 Firefox/10.0.2
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: es-MX,es;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Connection: keep-alive

不过,我不能发送连接头。这是我的代码:

However, I can not send Connection header. This is my code:

// request
HttpWebRequest request = CreateWebRequestObject(url);
request.CookieContainer = this.cc;
request.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:10.0.2) Gecko/20100101 Firefox/10.0.2";

// headers
request.Headers.Add("Accept-Encoding", "gzip, deflate");
request.Headers.Add("Accept-Language", " es-MX,es;q=0.8,en-us;q=0.5,en;q=0.3");
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
request.KeepAlive = true; // it does not work as expected
request.ServicePoint.Expect100Continue = false; // remove Expect header

// post
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = buffer.Length;

using (Stream stream = request.GetRequestStream())
  stream.Write(buffer, 0, buffer.Length);



但是,当我在提琴手检查请求的连接属性没有出现。

But, when I check the request in Fiddler the Connection property does not appears.

此外,这些职位不为我工作:

In addition, these posts does not work for me:


  1. Keep在C#中的HTTP连接活着吗?

  2. C# - 连接:保持活动的标头不发送HttpWebRequest的过程中

  1. Keep a http connection alive in C#?
  2. C# - Connection: keep-alive Header is Not Being Sent During HttpWebRequest

我如何正确地发送连接头?

How do I send Connection header correctly?

更新

这添加保持活动使用HTTP / 1.0

This add Keep-Alive using HTTP/1.0

request.ProtocolVersion = HttpVersion.Version10;
//request.KeepAlive = true;  // not necessary

在变化ProtocolVersion财产HttpVersion.Version11,保持活动的标头不发送:

When change ProtocolVersion property to HttpVersion.Version11, Keep-Alive header is not send:

request.ProtocolVersion = HttpVersion.Version11;
request.KeepAlive = true;



我如何发送使用HTTP / 1.1保持活动的头?

How can I send Keep-Alive header using Http/1.1?

推荐答案

在使用HTTP / 1.1,保持活动是由default.Setting的KeepAlive为false可能导致发送一个连接:关闭页眉到服务器

When using HTTP/1.1, Keep-Alive is on by default.Setting KeepAlive to false may result in sending a Connection: Close header to the server.

这篇关于如何正确发送保持连接头在C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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