Setting per request value for ServicePointManager.

2020-06-08 03:49发布

问题:

In c# I am able to set a static value for SSL3 or TLS, e.g.

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;

Or:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;

But (I believe) this will affect all future HttpWebRequest objects in my application.

Is there a way to set this for a given HttpWebRequest or at least for a given URI?

Note I have seen this:

Uri uri = new Uri(url);
ServicePoint sp = ServicePointManager.FindServicePoint(uri);

But ServicePoint does not have a SecurityProtocol property.

At present I am thinking I will have to just set the static global property prior to creating a new HttpWebRequest.

This doesn't feel right and it also means:

  • I have to make sure multiple threads are not doing this at the same time.
  • I am not sure by what point this setting has been 'used' (i.e. is it when I call webRequest.GetResponse() that the ServicePointManager.SecurityProtocol is accessed and bound to that URI?).

回答1:

Realised this has been covered here:

How to use SSL3 instead of TLS in a particular HttpWebRequest?

And here:

Set the SecurityProtocol (Ssl3 or TLS) on the .net HttpWebRequest per request

Confirming my fears. Some users appear to be spinning up a separate app domain to work around this. I still think it might be possible with some thread locking if it was well defined at what point the setting is bound to a particular web request object.