2

I am trying to connect to URL using WebClient inside Asp.Net Web Api project. My intention was to get the content and create a web site thumbnail.

Sample Code:

using (var client = new WebClient())
{
    var contents = client.DownloadString("http://www.google.com");
}

But this WebClient client object throws timeout exception in case of WebApi and same works fine for console application.

Kindly help.

Ravi Kanasagra
  • 591
  • 1
  • 8
  • 22
  • 1
    Who throws the timeout exception? Is it the MVC framework itself or the WebClient? I would try using the `WebClient.DownloadStringAsync` method. – Gábor Imre Nov 10 '14 at 14:34
  • The code line, var contents = client.DownloadString("http://www.google.com"); is throwing timeout exception. – Ravi Kanasagra Nov 11 '14 at 11:07
  • 1
    I'm having the same issue as this, or so it seems. I'm actually using RestSharp, and seeing the same pattern. Works in a console app, times out in MVC. I tried the above code as a simpler test, and the same happens. Any updates? For setup, this is running locally on my machine, debugging with Visual Studio. The sample code above also runs fine in LinqPad... tearing my hair out! – xan Nov 21 '14 at 14:11
  • @xan - I am still searching for solution. :( – Ravi Kanasagra Nov 21 '14 at 16:00
  • I've read some comments elsewhere to so with deadlocking in ASP applications do to restrictions on SyncronizationContext and thread locking. It doesn't feel right in this case since ' client.DownloadString()' is synchronous right? I tried switching to HttpClient instead of WebClient, thinking it was a little more bare metal and might give more information. Exact same issue, but just get a "Task Cancelled" exceptionn. – xan Nov 21 '14 at 16:08
  • I found the answer eventually - it was the default proxy not being set correctly. See my answer below. – xan Nov 27 '14 at 15:51

1 Answers1

1

I found the answer here in the end: Web Request through Proxy using RestSharp

Adding:

<system.net>
    <defaultProxy enabled="true" useDefaultCredentials="true"></defaultProxy>
</system.net>

To my web config fixed it for me.

Community
  • 1
  • 1
xan
  • 7,440
  • 8
  • 43
  • 65