0

Some websites I download through my c# app seem to throw an exception of

A call to SSPI failed

No matter what combination of SSL confiruration I try I can't see to get it to download anything from these websites. 3m.com is an example.

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.3m.com");

ServicePointManager.CheckCertificateRevocationList = false;
ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Ssl3 | System.Net.SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;

request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
request.Timeout = 5000;
request.KeepAlive = false;
request.AllowAutoRedirect = true;
request.Accept = "text/html, application/xhtml+xml, application/xml; q=0.9, image/webp, */*; q=0.8";
request.Headers["Accept-Language"] = "en-GB,en-US;q=0.7,en;q=0.3";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
Mr J
  • 2,655
  • 4
  • 37
  • 58
  • 2
    Your code works fine on my machine. The response text is readily available. It seems to be an issue with how your SChannel from Windows is configured. – Christoph Herold Mar 10 '19 at 19:14
  • Typically, this exception comes with an InnerException, that should give you some clues as to what is going wrong internally. – Christoph Herold Mar 10 '19 at 19:15
  • 1
    I'm on the same page as @ChristophHerold. jamie, take a look at this, may be helpful: https://stackoverflow.com/a/30479567/2206145 – yv989c Mar 10 '19 at 19:17
  • My inner is "Win32Exception: The function requested is not supported". No idea what that means ha! I'll try the reg edit thanks :-) – Mr J Mar 10 '19 at 19:29

1 Answers1

1

I figured this out in the end.

The problem was, I was declaring the SSL options AFTER creating the HttpWebRequest object.

Code below works.

ServicePointManager.CheckCertificateRevocationList = false;
ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Ssl3 | System.Net.SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
ServicePointManager.ServerCertificateValidationCallback += (senderj, cert, chain, sslPolicyErrors) => true;

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.3m.com");

request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
request.Timeout = 5000;
request.KeepAlive = false;
request.AllowAutoRedirect = true;
request.Accept = "text/html, application/xhtml+xml, application/xml; q=0.9, image/webp, */*; q=0.8";
request.Headers["Accept-Language"] = "en-GB,en-US;q=0.7,en;q=0.3";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
Mr J
  • 2,655
  • 4
  • 37
  • 58