0

I try request site page from ConsoleApp:

static void Main(string[] args) { ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; var handler = new HttpClientHandler { AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate , AllowAutoRedirect = false }; using (var client = new HttpClient(handler)) { var response = client.GetAsync("https://lk.fssprus.ru/ds_cabinet/action/login"); var httpResponseMessage = response.Result; var content = httpResponseMessage.Content.ReadAsStringAsync(); Console.WriteLine(content.Result); } Console.ReadKey(); } 

and get this error:

System.AggregateException HResult=0x80131500 Message=One or more errors occurred. Source=mscorlib StackTrace: at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions) at System.Threading.Tasks.Task1.GetResultCore(Boolean waitCompletionNotification) at System.Threading.Tasks.Task1.get_Result() at ConsoleApp8.Program.Main(String[] args) in C:\Projects\FsspConnectTest\ConsoleApp8\Program.cs:line 26

This exception was originally thrown at this call stack: System.Net.HttpWebRequest.EndGetResponse(System.IAsyncResult) System.Net.Http.HttpClientHandler.GetResponseCallback(System.IAsyncResult)

Inner Exception 1: HttpRequestException: An error occurred while sending the request.

Inner Exception 2: WebException: The request was aborted: Could not create SSL/TLS secure channel.

I can't resolve this problem

Update: I test on Windows 10 and it's work perfect, but on my Windows 8.1 not work. What need install for Windows 8.1?

7
  • Did you try ServicePointManager.Expect100Continue = true; ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3;? Commented Mar 25, 2020 at 12:24
  • What happens if you comment out the ServicePointManager line? In .NET Core you normally wouldn't need to set it explicitly. Commented Mar 25, 2020 at 12:47
  • @Selim Yıldız Yes, same problem Commented Mar 25, 2020 at 13:29
  • @Crowcoder .Net Core error: Message=One or more errors occurred. (The SSL connection could not be established, see inner exception.) Commented Mar 25, 2020 at 13:33
  • I assume you can go to that page in a browser? Commented Mar 25, 2020 at 14:07

1 Answer 1

1

There are two things -

  1. "In .NET Core, ServicePointManager affects only HttpWebRequest. It does not affect HttpClient. In .NET Framework, the built-in HttpClient is built on top of HttpWebRequest, therefore ServicePointManager settings will apply to it."

    So set security protocol in HttpClientHandler that you're passing into HttpClient constructor.

  2. Verify if the required TLS version is disabled on the machine. Since you said it works just fine in one place but fails in another and TLS1.2 is the default protocol enabled on Windows 8.1 it could be the case. So here is how you do it - Transport Layer Security (TLS) registry settings.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.