Skip to content

HttpClient exceptions are always culture specific #80424

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Symbai opened this issue Jan 10, 2023 · 12 comments
Closed

HttpClient exceptions are always culture specific #80424

Symbai opened this issue Jan 10, 2023 · 12 comments

Comments

@Symbai
Copy link

Symbai commented Jan 10, 2023

Description

I'm trying to get the exceptions messages of HttpClient in English but they are always in the current culture of Windows.

Reproduction Steps

using System.Globalization;

internal class Program
{
    private static void Main(string[] args)
    {
        Go();
        Console.ReadKey();
    }
    async static void Go()
    {
        Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
        Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
        CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("en-US");
        CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo("en-US");

        try
        {
            HttpResponseMessage webresponse = await new HttpClient().GetAsync("https://www.123blablablub.com");
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
    }
}

Expected behavior

The specified host is unknown. (www.123blablablub.com:443)

Actual behavior

Depending on the language of Windows, for example Spanish it would be:
El host especificado es desconocido. (www.123blablablub.com:443)

Regression?

No response

Known Workarounds

No response

Configuration

.NET 6, Windows 11, 64bit, Console / Winforms or WPF project

Other information

No response

@ghost
Copy link

ghost commented Jan 10, 2023

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Jan 10, 2023
@ghost
Copy link

ghost commented Jan 10, 2023

Tagging subscribers to this area: @dotnet/area-system-globalization
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

I'm trying to get the exceptions messages of HttpClient in English but they are always in the current culture of Windows.

Reproduction Steps

using System.Globalization;

internal class Program
{
    private static void Main(string[] args)
    {
        Go();
        Console.ReadKey();
    }
    async static void Go()
    {
        Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
        Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
        CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("en-US");
        CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo("en-US");

        try
        {
            HttpResponseMessage webresponse = await new HttpClient().GetAsync("https://www.123blablablub.com");
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
    }
}

Expected behavior

The specified host is unknown. (www.123blablablub.com:443)

Actual behavior

Depending on the language of Windows, for example Spanish it would be:
El host especificado es desconocido. (www.123blablablub.com:443)

Regression?

No response

Known Workarounds

No response

Configuration

.NET 6, Windows 11, 64bit, Console / Winforms or WPF project

Other information

No response

Author: Symbai
Assignees: -
Labels:

area-System.Globalization, untriaged

Milestone: -

@ghost
Copy link

ghost commented Jan 10, 2023

Tagging subscribers to this area: @dotnet/ncl
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

I'm trying to get the exceptions messages of HttpClient in English but they are always in the current culture of Windows.

Reproduction Steps

using System.Globalization;

internal class Program
{
    private static void Main(string[] args)
    {
        Go();
        Console.ReadKey();
    }
    async static void Go()
    {
        Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
        Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
        CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("en-US");
        CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo("en-US");

        try
        {
            HttpResponseMessage webresponse = await new HttpClient().GetAsync("https://www.123blablablub.com");
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
    }
}

Expected behavior

The specified host is unknown. (www.123blablablub.com:443)

Actual behavior

Depending on the language of Windows, for example Spanish it would be:
El host especificado es desconocido. (www.123blablablub.com:443)

Regression?

No response

Known Workarounds

No response

Configuration

.NET 6, Windows 11, 64bit, Console / Winforms or WPF project

Other information

No response

Author: Symbai
Assignees: -
Labels:

area-System.Net.Http, untriaged

Milestone: -

@ghost
Copy link

ghost commented Jan 10, 2023

Tagging subscribers to this area: @dotnet/area-system-resources
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

I'm trying to get the exceptions messages of HttpClient in English but they are always in the current culture of Windows.

Reproduction Steps

using System.Globalization;

internal class Program
{
    private static void Main(string[] args)
    {
        Go();
        Console.ReadKey();
    }
    async static void Go()
    {
        Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
        Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
        CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("en-US");
        CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo("en-US");

        try
        {
            HttpResponseMessage webresponse = await new HttpClient().GetAsync("https://www.123blablablub.com");
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
    }
}

Expected behavior

The specified host is unknown. (www.123blablablub.com:443)

Actual behavior

Depending on the language of Windows, for example Spanish it would be:
El host especificado es desconocido. (www.123blablablub.com:443)

Regression?

No response

Known Workarounds

No response

Configuration

.NET 6, Windows 11, 64bit, Console / Winforms or WPF project

Other information

No response

Author: Symbai
Assignees: -
Labels:

area-System.Resources, untriaged

Milestone: -

@tarekgh
Copy link
Member

tarekgh commented Jan 10, 2023

This is not a globalization issue. This needs to be decided and handled on the level of the exception throwing and resource handling. Currently this is not supported except setting the current UI culture to English.

@tarekgh
Copy link
Member

tarekgh commented Jan 10, 2023

This is a duplicate of #40427

@Symbai
Copy link
Author

Symbai commented Jan 10, 2023

This is a duplicate of #40427

I dont think so. #40427 says I can set UI culture to English using CultureInfo.DefaultThreadCurrentUICulture etc. which I am doing in my example. It still doesn't work, but only for HttpClient. It works for all other exceptions, so no duplicate.

@tarekgh
Copy link
Member

tarekgh commented Jan 10, 2023

@Symbai what exception you are getting in the case you mentioned? Is it Win32Exception? If so, then it is same as #40427. Just start read it from the comment #40427 (comment)

@Symbai
Copy link
Author

Symbai commented Jan 10, 2023

Its HttpClient, so its System.Net.Http.HttpRequestException, not a Win32Exception. I cannot even cast it to a Win32Exception so I cannot use FormatMessageW. I still don't think its a duplicate.

@tarekgh
Copy link
Member

tarekgh commented Jan 10, 2023

Your code throws the following:

System.Net.Http.HttpRequestException: No such host is known. (www.123blablablub.com:443)
 ---> System.Net.Sockets.SocketException (11001): No such host is known.
   at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken)
   at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(Int16 token)
   at System.Net.Sockets.Socket.<ConnectAsync>g__WaitForConnectWithCancellation|281_0(AwaitableSocketAsyncEventArgs saea, ValueTask connectTask, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.ConnectToTcpHostAsync(String host, Int32 port, HttpRequestMessage initialRequest, Boolean async, CancellationToken cancellationToken)

So, it throws System.Net.Sockets.SocketException which is extending Win32Exception.

It is same thing discussed in #40427 (comment). Basically, the message is coming from Windows OS and not from .NET. Windows cannot guarantee to carry English resources in all situations.

There is some manual workaround here https://gist.github.com/ericstj/c72b90b0c12f86b7918850ee276fac3b which can help get the English message (if Windows has English resources). You can just pick SocketException.NativeErrorCode and call the extension method ToString from the work around using English culture.

@tarekgh
Copy link
Member

tarekgh commented Jan 10, 2023

I cannot even cast it to a Win32Exception

HttpResponseMessage.InnerException will have the SocketException. Just ensure to check HttpResponseMessage.InnerException is not null and is SocketException.

@Symbai
Copy link
Author

Symbai commented Jan 11, 2023

The workaround expected a Win32Exception as parameter so I tried casting it which obviously didn't worked. Didn't know about SocketException and native code field. I had to modify the workaround, now it works. Closing as duplicate.

@Symbai Symbai closed this as completed Jan 11, 2023
@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Jan 11, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Feb 10, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants