How to force ipv6 or ipv4 for HttpWebRequest or WebRequest C#

后端 未结 1 1651
小蘑菇
小蘑菇 2021-01-14 07:23

Coming from node.js I can do this to tell node.js to make the request using ipv6 vs ipv4

var http = require(\"http\");
var options = {
  hostname: \"google.c         


        
相关标签:
1条回答
  • 2021-01-14 08:07

    You can use ServicePoint.BindIPEndPointDelegate.

    var req = HttpWebRequest.Create(url) as HttpWebRequest;
    
    req.ServicePoint.BindIPEndPointDelegate = (servicePoint, remoteEndPoint, retryCount) =>
    {
        if (remoteEndPoint.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)
        {
            return new IPEndPoint(IPAddress.IPv6Any, 0);
        }
    
        throw new InvalidOperationException("no IPv6 address");
    };
    
    0 讨论(0)
提交回复
热议问题