C
C#2w ago
bhavish

✅ Request error: The SSL connection could not be established, see inner exception.

using System.Text;

var client = new HttpClient();

// Set up the request
var request = new HttpRequestMessage
{
Method = HttpMethod.Post,
RequestUri = new Uri("https://<my-domain>.azurewebsites.net/company/products/notifier/email"),
Content = new StringContent(
@"{
""fromMail"": ""[email protected]"",
""fromName"": ""IHRM-Team"",
""to"": [""[email protected]""],
""subject"": ""some message"",
""templateId"": ""d-401bd3782f974c1da24db450f5f1eb86"",
""props"": {
""name"": ""GUS""
}
}",
Encoding.UTF8,
"application/json"
)
};

// Set headers
request.Headers.Add("accept", "*/*");

try
{
// Send the request
var response = await client.SendAsync(request);

// Ensure the response indicates success
response.EnsureSuccessStatusCode();

// Print the response content
var responseContent = await response.Content.ReadAsStringAsync();
Console.WriteLine($"Response: {responseContent}");
}
catch (HttpRequestException e)
{
Console.WriteLine($"Request error: {e.Message}");
}
using System.Text;

var client = new HttpClient();

// Set up the request
var request = new HttpRequestMessage
{
Method = HttpMethod.Post,
RequestUri = new Uri("https://<my-domain>.azurewebsites.net/company/products/notifier/email"),
Content = new StringContent(
@"{
""fromMail"": ""[email protected]"",
""fromName"": ""IHRM-Team"",
""to"": [""[email protected]""],
""subject"": ""some message"",
""templateId"": ""d-401bd3782f974c1da24db450f5f1eb86"",
""props"": {
""name"": ""GUS""
}
}",
Encoding.UTF8,
"application/json"
)
};

// Set headers
request.Headers.Add("accept", "*/*");

try
{
// Send the request
var response = await client.SendAsync(request);

// Ensure the response indicates success
response.EnsureSuccessStatusCode();

// Print the response content
var responseContent = await response.Content.ReadAsStringAsync();
Console.WriteLine($"Response: {responseContent}");
}
catch (HttpRequestException e)
{
Console.WriteLine($"Request error: {e.Message}");
}
I am using a mail sender API which is working in other language scripts and postman but not in C#. and giving the above mentioned error
15 Replies
Honza K.
Honza K.2w ago
Show us the exception + inner exceptions... Most likely the server's SSL certificate is not trusted on your machine
bhavish
bhavishOP2w ago
How do i make it to trust
Unhandled exception. System.Net.Http.HttpRequestException: The SSL connection could not be established, see inner exception.
---> System.Security.Authentication.AuthenticationException: Authentication failed, see inner exception.
---> Interop+AppleCrypto+SslException: bad protocol version
--- End of inner exception stack trace ---
at System.Net.Security.SslStream.ForceAuthenticationAsync[TIOAdapter](Boolean receiveFirst, Byte[] reAuthenticationData, CancellationToken cancellationToken)
at System.Net.Http.ConnectHelper.EstablishSslConnectionAsync(SslClientAuthenticationOptions sslOptions, HttpRequestMessage request, Boolean async, Stream stream, CancellationToken cancellationToken)
--- End of inner exception stack trace ---
at System.Net.Http.ConnectHelper.EstablishSslConnectionAsync(SslClientAuthenticationOptions sslOptions, HttpRequestMessage request, Boolean async, Stream stream, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.CreateHttp11ConnectionAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.AddHttp11ConnectionAsync(QueueItem queueItem)
at System.Threading.Tasks.TaskCompletionSourceWithCancellation`1.WaitWithCancellationAsync(CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.SendWithVersionDetectionAndRetryAsync(HttpRequestMessage request, Boolean async, Boolean doRequestAuth, CancellationToken cancellationToken)
at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.
Unhandled exception. System.Net.Http.HttpRequestException: The SSL connection could not be established, see inner exception.
---> System.Security.Authentication.AuthenticationException: Authentication failed, see inner exception.
---> Interop+AppleCrypto+SslException: bad protocol version
--- End of inner exception stack trace ---
at System.Net.Security.SslStream.ForceAuthenticationAsync[TIOAdapter](Boolean receiveFirst, Byte[] reAuthenticationData, CancellationToken cancellationToken)
at System.Net.Http.ConnectHelper.EstablishSslConnectionAsync(SslClientAuthenticationOptions sslOptions, HttpRequestMessage request, Boolean async, Stream stream, CancellationToken cancellationToken)
--- End of inner exception stack trace ---
at System.Net.Http.ConnectHelper.EstablishSslConnectionAsync(SslClientAuthenticationOptions sslOptions, HttpRequestMessage request, Boolean async, Stream stream, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.CreateHttp11ConnectionAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.AddHttp11ConnectionAsync(QueueItem queueItem)
at System.Threading.Tasks.TaskCompletionSourceWithCancellation`1.WaitWithCancellationAsync(CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.SendWithVersionDetectionAndRetryAsync(HttpRequestMessage request, Boolean async, Boolean doRequestAuth, CancellationToken cancellationToken)
at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.
Honza K.
Honza K.2w ago
Interop+AppleCrypto+SslException: bad protocol version are you expected to use TLS v1.3? AFAIK it is not supported on macOS
bhavish
bhavishOP2w ago
idk, it works on postman (cURL) and other language scirpts is there some way to bypass this?
Honza K.
Honza K.2w ago
you can try this new HttpClient(new SocketsHttpHandler() { SslOptions = { EnabledSslProtocols = System.Security.Authentication.SslProtocols.Tls12 } });
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
Honza K.
Honza K.2w ago
I'm not sure this would help with the posted error... that is why I posted the link to migration FROM web request as it shows how to use the SocketsHttpHandler... not saying anybody should use http web request in modern .net
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
Honza K.
Honza K.2w ago
yeah but the link shows how to use the sockets handler 😄
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
Honza K.
Honza K.2w ago
I had no clue you can change the TLS version like this
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
Honza K.
Honza K.2w ago
If I knew you can change it in project settings I would say that lol
Unknown User
Unknown User7d ago
Message Not Public
Sign In & Join Server To View

Did you find this page helpful?