How to turn off the automatic proxy detection in t

2019-04-27 01:21发布

When using the AmazonS3 object for the first time after the application starts, there is a large delay of approx 14 seconds. This large time delay is not present for all subsequent calls.

I have encountered this exact delay issue before with other HTTP related classes and it is caused when the class in question tries to determine the local machine's proxy settings and whether to use them or not.

To stop this from happening with WebClient, you set WebClient.Proxy = null; and it doesn't try to auto detect the proxy settings, but I cannot figure out how to disable the proxy detection functionality of the AmazonS3 object.

I have specifically tried setting the ProxyHost to null:

_s3Client = AWSClientFactory.CreateAmazonS3Client(awsAccessKey, awsSecretAccessKey, new AmazonS3Config { ProxyHost = null });

Which didn't work. We are currently using the Amazon .NET SDK 'v1.3.17.0'.

Is there a way to turn off the proxy detection?

1条回答
The star\"
2楼-- · 2019-04-27 01:30

Good question - I haven't tried it myself and only analyzed the code, but the AmazonS3Config Class uses a private method configureWebRequest(), which in turn relies on the WebRequest Class to handle the actual HTTP connection. Now, WebRequest has a WebRequest.DefaultWebProxy Property , which is public static (i.e. you can set this within your application before calling CreateAmazonS3Client(()) :

The DefaultWebProxy property gets or sets the global proxy. The DefaultWebProxy property determines the default proxy that all WebRequest instances use if the request supports proxies and no proxy is set explicitly using the Proxy property. [emphasis mine]

The proxy auto detection you are experiencing is supposedly induced by the respective IE behavior:

The DefaultWebProxy property reads proxy settings from the app.config file. If there is no config file, the current user's Internet Explorer (IE) proxy settings are used.

Consequently I'd hope that this can be disabled in a similar way as for the WebClient.Proxy Property you mentioned, albeit on the class level, as indeed strongly suggested by the last paragraph:

If the DefaultWebProxy property is set to null, all subsequent instances of the WebRequest class created by the Create or CreateDefault methods do not have a proxy. [emphasis mine]


Added by blexandre

sample code from this answer would be

System.Net.WebRequest.DefaultWebProxy = null;
_s3Client = AWSClientFactory.CreateAmazonS3Client(awsAccessKey, awsSecretAccessKey);

Note that this will disable the proxy for every web request, _client is created using it, so it is safe to do this, but be careful if you might have more requests pending from the WebRequest class

查看更多
登录 后发表回答