WebClient.DownloadData times out when it shouldn't

Prueba2306 1 Reputation point
2022-04-11T19:22:39.387+00:00

In an ASP.NET MVC webapp I developed years ago in C#, there is an action / controller that downloads an image of Google maps and saves it to disk for later use, using WebClient.DownloadData.

This action / controller is requested 4 or 5 times a day. This web app (and the action / controller) worked perfectly in development and production (in-house server, Windows Server 2008 R2 Foundation, IIS 7.5, .NET Framework v4.0).

Recently, the app was moved to a new virtual server in AWS (Windows Server 2019 Datacenter, IIS 10, .NET CLR v4.0). The whole web app seems to work fine except for the action / controller that I mentioned previously.

If I use the URL in chrome installed in the new virtual server the map is downloaded without issues. But when the action / controller is requested it times out (exception):
191992-image.png

The code is simple:

string url = "http://maps.googleapis.com/maps/api/staticmap?key=[API_KEY]&center=" + lat + "," + long + "&size=355x255&markers=color:red|" + lat + "," + long;  
WebClient web = new WebClient();  
byte[] bs = web.DownloadData(url);  
// save bs in disk  

This is an example of a map that it should be downloaded

http://maps.googleapis.com/maps/api/staticmap?key=[API_KEY]&center=25.670984,-100.2850  

191993-image.png

I checked the WebClient.DownloadData documentation and I haven't found any hint of what can cause the problem.

Again, this action / controller worked perfectly fine for years but after the migration it started to fail.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,237 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Michael Taylor 47,966 Reputation points
    2022-04-11T19:37:08.497+00:00

    My theory is that you missed a configuration when you set up the new machine. But where that is could be hard to track down without being on that server.

    My first thought is that you're using Google API so your old server probably had your Google configuration information (API key, auth settings, etc) in a file on the file system somewhere and the new app doesn't have it. Therefore the call is going to Google (or failing outright) and you're stuck in a loop. I would build a simple test client that makes that same call and see if it works. If it fails then it is a Google configuration issue.


  2. Prueba2306 1 Reputation point
    2022-04-13T02:22:23.817+00:00

    Found the problem: was the default proxy of the server.

    Thanks to this question How to bypass the system proxy when using WebClient the path to the solution was there.

    WebClient webclient = new WebClient();  
    webclient.Proxy = null;  
    

    According to the WebClient.Proxy Property documentation:

    The proxy is set by the system using configuration files and the Internet Explorer Local Area Network settings. To specify that no proxy should be used, set the Proxy property to null.

    And the default proxy of the server (AWS) does not allow the request that I was trying to perform.

    Now, because I can not modify the code (right now) in the webapp (action/controller), I had to search How to disable proxy server via web.config file and it worked.

    <system.net>  
      <defaultProxy>  
        <proxy usesystemdefault="False"/>  
      </defaultProxy>  
    </system.net>  
    

    Obviously, the value was "True" and changed it to "False".

    0 comments No comments