0

I'm using the WebRequest class to make calls to a web service.

I want to set the KeepAlive property to 'false' but i'm not finding this attribute.

How can I do it?

marcus
  • 395
  • 5
  • 22
  • `KeepAlive` is a property that belongs to [HttpWebRequest](https://learn.microsoft.com/en-us/dotnet/api/system.net.httpwebrequest), the class object generated when a new WebRequest is created: `HttpWebRequest request = WebRequest.CreateHttp(URI);`. `KeepAlive` is usually set to `true` in Http 1.1 and is ignored in Http 2.0, since it's the default and you cannot change it. Http 1.0 (if you can find something that uses it somewhere), can be set to `false` (then, it depends on you what happens). If you set `KeepAlive = false`, it can be interpreted as `ConnectionClose`. – Jimi Jul 17 '20 at 16:50
  • Btw, you don't *create* a connection, you ask for one, it's the `ServicePoint` assigned to your connection request that handles this. `KeepAlive` is also part of the transaction performed in the handshake. Why do you think you need to set `KeepAlive = false`? – Jimi Jul 17 '20 at 16:56

1 Answers1

1

As Microsoft has mentioned. Source

We advise not to use WebRequest or its derivative classes for the new development. Instead, use the System.Net.Http.Http.HttpClientclass.

WebRequest is the basic abstract class for. NET request/response model to access data from the Internet. So you can use HttpClient instead that has KeepAlive property.

C# How to set HttpClient Keep-Alive to false
https://learn.microsoft.com/fr-fr/dotnet/api/system.net.http.httpclient?view=netcore-3.1

Hammas
  • 1,126
  • 1
  • 12
  • 37
  • This is helpful, although the quoted documentation of unknown source is confusing: first "We advise ... to use WebRequest" but then "Instead, use ... HttpClient". It seems like that first sentence is missing "_not_ to use". The [English documentation for `WebRequest`](https://learn.microsoft.com/dotnet/api/system.net.webrequest) reads "We don't recommend that you use WebRequest or its derived classes for new development." and translating the French version I get "We do not recommend using WebRequest or its derived classes for new development." – Lance U. Matthews Jul 17 '20 at 16:21
  • **Instead, use the System.Net.Http.HttpClient class.** that's why i said use `httpClient` – Hammas Jul 17 '20 at 16:27
  • yeah the statement is translated and i copied. from OPs linked source. – Hammas Jul 17 '20 at 16:30
  • Right, but you see that the first sentence is saying "to use" — not "not to use" — `WebRequest`? The first sentence says "use WebRequest" and then the second says "use ... HttpClient." That's why I said it's confusing. – Lance U. Matthews Jul 17 '20 at 16:30
  • Thank you for your response, but I want to know if there is a solution if we want to keep using WebRequest class. – marcus Jul 17 '20 at 16:35
  • @marcus i don't think so. You'll need to use implementation of it that is `httpWebRequest` – Hammas Jul 17 '20 at 16:47
  • 1
    You could access the respective headers via `request.Headers[HttpRequestHeader.Connection]` and `request.Headers[HttpRequestHeader.KeepAlive]`, although [the documentation](https://learn.microsoft.com/dotnet/api/system.net.webheadercollection) says that the `Connection` header is "restricted". How are you using `WebRequest`, though? It's an abstract class, so ultimately you have to be using something that's a derived class. Maybe you're already using `HttpWebRequest` and just need to cast it? That class exposes proper `Connection` and `KeepAlive` properties. – Lance U. Matthews Jul 17 '20 at 16:56