You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
UPDATE: Scoped to WebSocket only as there is no way to achieve that. HttpClient has a way and HttpWebRequest is legacy API (so no need).
I find the sulution for httpclient from issues like this.
varhandler=newSocketsHttpHandler{ConnectCallback=async(context,token)=>{// dns get ipv6 and connectreturnipv6stream}};varclient=newHttpClient(handler);// HttpClientHandle???
but it can useful for httpclient only,and it replaced httpclienthandle,will lost the diagnosticsHandler yet.
and I view the httpwebrequest and websocket source code, the SocketsHttpHandler is a private member of them only,and i can't get the ConnectCallback for using.
so how can i force ipv6 or ipv4 on httpwebrequest, websocket and httpclient(with diagnosticsHandler )
There's no way to do this with HttpWebRequest or WebSocket.
The former is obsolete and not being invested in. We could consider an API for WebSocket, though.
Not being able to use DiagnosticsHandler with SocketsHttpHandler is a known wart -- tracking that at #31261
Except DiagnosticsHandler,for replace the HttpWebRequest,I can use HttpClient via ConnectCallback to implement a ipv6 web request , I'm really looking forward to a similar API for 'WebSocket' as well.
Except DiagnosticsHandler,for replace the HttpWebRequest,I can use HttpClient via ConnectCallback to implement a ipv6 web request , I'm really looking forward to a similar API for 'WebSocket' as well.
Actually, you can use WebSocket.CreateFromStream here, so I don't think any new API is needed.
Except DiagnosticsHandler,for replace the HttpWebRequest,I can use HttpClient via ConnectCallback to implement a ipv6 web request , I'm really looking forward to a similar API for 'WebSocket' as well.
Actually, you can use WebSocket.CreateFromStream here, so I don't think any new API is needed.
If I use WebSocket.CreateFromStream,it means i must copy all code of 'WebSocketHandle.ConnectAsync',and add 'ConnectCallback' to 'SocketsHttpHandler',to completed the websocket protocol upgrade manual,just for use ConnectCallback
only.
does it has a simple implement such as add the 'SocketsHttpHandler' or 'ConnectCallback' member to 'ClientWebSocketOptions'.
changed the title [-]when i use tcp connection via httpwebrequest, websocket and httpclient,how can i force ipv6 or ipv4[/-][+]When I use TCP connection via HttpWebRequest, WebSocket and HttpClient, how can I force IPv6 or IPv4[/+]on Dec 8, 2020
Triage: @xinbingyiran how much code is that? Is that inconveniently long code?
Yes, It's not,but...
At present,if a developer need to switch a option for using ipv4 or ipv6,he must view the source code of runtime, to find the logic of connection(it may be a long time),and then copy all of them to his own code and change it carefully.
Only for using ipv6 or ConnectCallback of SocketsHttpHandler.do you think it's a better way than to offer an option?
And there is a SocketsHttpHandler for using in ClientWebSocketOptions,why not try exposing it or its callback method to the developer?
In general case, this should not be needed, right @xinbingyiran? If the service is not IPv6 capable, it should not be in DNS in first place. If IPv6 is not available on the client it should fall-back to IPv4. The ConnectCallback was added primarily for use-cases other than this.
While WebSocket may be interesting , HttpWebRequest is viewed as obsolete and it will be official marked as such soon.
What if I need to connect to a 'WebSocket' server that supports both IPv4 and IPv6(e.g. ws://localhost/) over the IPv4 protocol? Just like I need to make an IPv4 Web request, @wfurt .
At some point we had discussed allowing you to specify and configure the instance of SocketsHttpHandler that is used by the WebSocket. Seems like that would allow you to address this scenario.
At some point we had discussed allowing you to specify and configure the instance of SocketsHttpHandler that is used by the WebSocket. Seems like that would allow you to address this scenario.
Activity
scalablecory commentedon Dec 3, 2020
There's no way to do this with
HttpWebRequestorWebSocket.The former is obsolete and not being invested in. We could consider an API for
WebSocket, though.Not being able to use
DiagnosticsHandlerwithSocketsHttpHandleris a known wart -- tracking that at #31261xinbingyiran commentedon Dec 4, 2020
Except
DiagnosticsHandler,for replace theHttpWebRequest,I can useHttpClientviaConnectCallbackto implement a ipv6 web request , I'm really looking forward to a similar API for 'WebSocket' as well.scalablecory commentedon Dec 4, 2020
Actually, you can use
WebSocket.CreateFromStreamhere, so I don't think any new API is needed.xinbingyiran commentedon Dec 4, 2020
If I use
WebSocket.CreateFromStream,it means i must copy all code of 'WebSocketHandle.ConnectAsync',and add 'ConnectCallback' to 'SocketsHttpHandler',to completed the websocket protocol upgrade manual,just for use ConnectCallbackonly.
does it has a simple implement such as add the 'SocketsHttpHandler' or 'ConnectCallback' member to 'ClientWebSocketOptions'.
[-]when i use tcp connection via httpwebrequest, websocket and httpclient,how can i force ipv6 or ipv4[/-][+]When I use TCP connection via HttpWebRequest, WebSocket and HttpClient, how can I force IPv6 or IPv4[/+]karelz commentedon Dec 8, 2020
Triage: @xinbingyiran how much code is that? Is that inconveniently long code?
xinbingyiran commentedon Dec 9, 2020
Yes, It's not,but...
At present,if a developer need to switch a option for using
ipv4oripv6,he must view the source code of runtime, to find the logic of connection(it may be a long time),and then copy all of them to his own code and change it carefully.Only for using
ipv6orConnectCallbackofSocketsHttpHandler.do you think it's a better way than to offer an option?And there is a
SocketsHttpHandlerfor using inClientWebSocketOptions,why not try exposing it or its callback method to the developer?wfurt commentedon Dec 9, 2020
In general case, this should not be needed, right @xinbingyiran? If the service is not IPv6 capable, it should not be in DNS in first place. If IPv6 is not available on the client it should fall-back to IPv4. The ConnectCallback was added primarily for use-cases other than this.
While WebSocket may be interesting , HttpWebRequest is viewed as obsolete and it will be official marked as such soon.
xinbingyiran commentedon Dec 9, 2020
What if I need to connect to a 'WebSocket' server that supports both IPv4 and IPv6(e.g. ws://localhost/) over the IPv4 protocol? Just like I need to make an IPv4 Web request, @wfurt .
Since HTTP is supported, how about WebSocket?
6 remaining items
antonfirsov commentedon Feb 9, 2021
@xinbingyiran I'm wondering if you can elaborate a bit on your use-case.
In #47583 we are proposing a switch to globally disable IPv6, is this something you would find useful?
geoffkizer commentedon Feb 9, 2021
At some point we had discussed allowing you to specify and configure the instance of SocketsHttpHandler that is used by the WebSocket. Seems like that would allow you to address this scenario.
CarnaViire commentedon Aug 12, 2025
Triage: This is now possible, you can use a custom configured
HttpMessageInvoker(orHttpClient) instance in the ClientWebSocket.ConnectAsync(Uri, HttpMessageInvoker, CancellationToken) overload (introduced in .NET 7).Closing as completed.