Skip to content

HttpClient not support SOCKS5H proxy #123217

Closed

Description

@itbencn

Description

   在 System.Net.Http.HttpConnectionPoolManager.SendAsync(HttpRequestMessage request, Boolean async, Boolean doRequestAuth, CancellationToken cancellationToken)
   在 System.Net.Http.HttpConnectionHandler.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
   在 System.Net.Http.HttpMessageHandlerStage.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   在 System.Net.Http.Metrics.MetricsHandler.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
   在 System.Net.Http.HttpMessageHandlerStage.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   在 System.Net.Http.DiagnosticsHandler.<SendAsyncCore>d__10.MoveNext()
   在 System.Runtime.CompilerServices.ConfiguredValueTaskAwaitable`1.ConfiguredValueTaskAwaiter.GetResult()
   在 System.Net.Http.RedirectHandler.<SendAsync>d__4.MoveNext()
   在 System.Net.Http.SocketsHttpHandler.<<SendAsync>g__CreateHandlerAndSendAsync|115_0>d.MoveNext()
   在 System.Net.Http.HttpClient.<GetStringAsyncCore>d__41.MoveNext()
   在 ConsoleApp1.Program.<Main>d__0.MoveNext() 在 C:\Users\ali\Desktop\ConsoleApp1\ConsoleApp1\Program.cs 中: 第 15 行


Only the 'http', 'https', 'socks4', 'socks4a' and 'socks5' schemes are allowed for proxies.

Reproduction Steps

using var handler = new SocketsHttpHandler();
handler.Proxy = new WebProxy("socks5h://username:password@ip:port");
using HttpClient client = new HttpClient(handler);
var response = await client.GetStringAsync("http://ifconfig.me/ip");

Expected behavior

normal request

Actual behavior

throw an exception

Only the 'http', 'https', 'socks4', 'socks4a' and 'socks5' schemes are allowed for proxies.

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Activity

  1. added
    untriaged
    on Jan 15, 2026
  2. dotnet-policy-service commented on Jan 15, 2026

    @dotnet-policy-service
    Contributor

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

  3. MihaZupan commented on Jan 15, 2026

    @MihaZupan
    Member

    https://en.wikipedia.org/wiki/SOCKS

    Since clients are allowed to use either resolved addresses or domain names, a convention from cURL exists to label the domain name variant of SOCKS5 "socks5h", and the other simply "socks5". A similar convention exists between SOCKS4a and SOCKS4.

    TIL socks5h is a thing.
    I think it'd be fine to add with the same behavior as our socks5 support, but I would not change our socks5 logic to resolve addresses locally. We can treat both the same and always forward the host to the proxy, the user always has the option to do the name resolution locally before making the request.

  4. wfurt commented on Jan 15, 2026

    @wfurt
    Member

    Is that something we want to do for 11 @MihaZupan ? Or set it to Future and do it opportunistically?

  5. MihaZupan commented on Jan 15, 2026

    @MihaZupan
    Member

    Normally I'd say Future, but since Copilot one-shot the whole change in #123218 I don't see much harm in adding it in 11.

  6. added this to the 11.0.0 milestone on Jan 15, 2026
  7. removed
    untriaged
    on Jan 15, 2026
  8. itbencn commented on Jan 17, 2026

    @itbencn
    Author

    Great, I have seen the corresponding PR modification records, but it seems that it will take a long time to officially use this feature at present Is there any alternative way to use SOCKS5H in the .NET 10 distribution

  9. MihaZupan commented on Jan 17, 2026

    @MihaZupan
    Member

    Just change the Url to socks5 instead. They behave the same in our implementation.

  10. itbencn commented on Jan 17, 2026

    @itbencn
    Author

    Just change the Url to socks5 instead. They behave the same in our implementation.

    I tried to change SOCKS5H to SOCKS5 in NET10 , but encountered an error in

    An error occurred while establishing a connection to the proxy tunnel.
    SOCKS server failed to connect to the destination. Received error code 0x02.
    

    I also tried to change SOCKS5H to SOCKS5 through the libcurl library, but it still reported an error proxy handshake error

    In summary, it cannot be directly modified and seems to be related to the proxy server or protocol.

    I don't mind providing a testable SOCKS5H proxy, but it requires configuring an IP whitelist.

  11. MihaZupan commented on Jan 17, 2026

    @MihaZupan
    Member

    If you can't connect with either .NET or Curl it's likely an issue with your proxy instead.
    The spec says error 2 is X'02' connection not allowed by ruleset, I'd check what the server thinks about your connections.

  12. itbencn commented on Feb 11, 2026

    @itbencn
    Author

    @MihaZupan
    I just used the NET11 preview SDK for testing
    Found using new WebProxy("socks5h://username:password@ip:port") Return 0x02 when in mode
    But use handler.Proxy.Credetials=new NetworkCredentials ("username", "password");Request normally when displaying configured credentials
    The problem exists for both SOCKS5 and SOCKS5H

    May I ask what caused this

  13. MihaZupan commented on Feb 11, 2026

    @MihaZupan
    Member

    Simply the fact that we never look at the UserInfo of the proxy url you set via WebProxy, we only look at the Credentials.

  14. itbencn commented on Feb 11, 2026

    @itbencn
    Author

    I can understand it as new WebProxy ("socks5h://username:password@ip:port") is only responsible for parsing the proxy address part and ignores the authentication information carried

    The behavior I thought before was that the authentication information carried would be processed together during parsing

  15. itbencn commented on Feb 14, 2026

    @itbencn
    Author

    new WebProxy ("socks5h://username:password@ip:port") will the authentication information carried in the URI string not be automatically assigned to the handler.Proxy.Cedetials object ?

  16. MihaZupan commented on Feb 14, 2026

    @MihaZupan
    Member

    No, you have to set the Credentials yourself.

    We could consider changing that (extract it from the Uri), but this has been the behavior for a long time.

  17. itbencn commented on Feb 14, 2026

    @itbencn
    Author

    It is more reasonable to automatically assign authentication information to Credentials when it is carried in the URI, otherwise the way Uri string parsing is very limited. Do I need to initiate a new issue?

  18. locked and limited conversation to collaborators on Mar 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions