10

I've got a very simple need - my C# code needs to connect to a remote server via HTTP and download a string. A trivial GET request, nothing more.

To ensure that my application remains responsive I also want to impose a timeout (say, 3 seconds) on the operation.

My first thought was to use System.Net.WebClient, but that doesn't support any timeouts.

Then I wanted to try the good old System.Net.HttpWebRequest, but alas - since .NET 4.5 it's been marked as obsolete!

So, what can I use? I checked out the System.Net.Http namespace, but it only allows asynchronous usage, forces to use Tasks, and generally only adds a dozen different layers of abstraction without really adding any new functionality (since it uses the same old System.Net.HttpWebRequest underneath anyway)

I don't want asynchronous stuff, I don't want to involve other threads, I don't want to involve the Tasks framework, I don't want tons of wrappers.

What is the correct way to do this in .NET 4.5?

Vilx-
  • 104,512
  • 87
  • 279
  • 422
  • 7
    It's not the `HttpWebRequest` that is deprecated, it's its constructors. `Do not use the HttpWebRequest constructor. Use the WebRequest.Create method to initialize new HttpWebRequest objects.` – GSerg Jan 31 '13 at 13:52
  • 2
    You could still use WebClient async methods to set a timeout: http://stackoverflow.com/a/601899/54937 – Sedat Kapanoglu Jan 31 '13 at 13:52
  • And you can always shift calls to `WebClient` to a background thread and use a semaphore of some kind (like an `AutoResetEvent`) that does have a timeout. –  Jan 31 '13 at 13:54
  • @Will - I already wrote that I want to keep it simple and avoid burning another thread needlessly. – Vilx- Jan 31 '13 at 13:55
  • 3
    @GSerg - perfect! Please make this into an answer so that I can accept it! – Vilx- Jan 31 '13 at 13:56
  • I think that [this answer](http://stackoverflow.com/a/3052637/1386111) is quite good. You can use it just like a `WebClient` and also set the timeout. – Alvin Wong Jan 31 '13 at 13:59
  • Burning a thread? Is this 1996? –  Jan 31 '13 at 14:00
  • 2
    If you want to make your application responsive, you *should* use “asynchronous stuff”. – svick Jan 31 '13 at 14:07
  • When Anders Hejlsberg, the guy who's the lead architect on the C# language since 2000, tells you to give `async` and `await` a try, you could at least consider it ;-). [The Future of C# and Visual Basic](http://channel9.msdn.com/Events/PDC/PDC10/FT09). Come on, do it for Anders... – Wim Ombelets Jan 31 '13 at 20:35
  • @WimOmbelets - Haha, funny! :D But, seriously, I do not see the reason to move processing to another thread if I block the first thread in the meantime. – Vilx- Feb 01 '13 at 08:28

2 Answers2

14

HttpWebRequest class is not deprecated, only its constructors are.

To cite the documentation:

Do not use the HttpWebRequest constructor. Use the WebRequest.Create method to initialize new HttpWebRequest objects. If the scheme for the Uniform Resource Identifier (URI) is http:// or https://, Create returns an HttpWebRequest object.

GSerg
  • 76,472
  • 17
  • 159
  • 346
2

Only the constructor is deprecated, not the HttpWebRequest class itself. Use WebRequest.Create to create an instance of HttpWebRequest.

dgvid
  • 26,293
  • 5
  • 40
  • 57