How to send KeepAlive header in C # correctly?

I need to send such a request 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 cannot send the 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 appear.

In addition, these messages do not work for me:

How to send the connection header correctly?

UPDATE

This adds Keep-Alive using HTTP / 1.0.

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

When the ProtocolVersion property changes to HttpVersion.Version11, the Keep-Alive header is not sent:

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

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

+5
source share
2 answers

HTTP/1.1 Keep-Alive . KeepAlive false Connection: Close .

+3

,

var sp = req.ServicePoint;
var prop = sp.GetType().GetProperty("HttpBehaviour", BindingFlags.Instance | BindingFlags.NonPublic);
prop.SetValue(sp, (byte)0, null);

DID . , httpwebrequest?

+1

All Articles