本文介绍了如何为HttpWebRequest或WebRequest C#强制使用ipv6或ipv4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
来自node.js,我可以这样做来告诉node.js使用ipv6 vs ipv4进行请求
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.com",
family: 4, // set to 6 for ipv6
};
var req = http.request(options, function(res) {
.. handle result here ..
});
req.write("");
req.end();
将family
设置为4
会强制ipv4,将其设置为6
会强制ipv6.如果不设置它,任何一个都可以工作.
Setting family
to 4
forces ipv4, setting it to 6
forces ipv6. Not setting it lets either work.
如何在C#(.NET 3.5)中执行相同的操作
How can I do the same thing in C# (.NET 3.5)
我可以想到一种方法,自己为自己的A或AAAA记录发出DNS请求,提出直接IP请求并设置host:
标头.有更好的方法吗?
I can think of one way which is to make a DNS request myself myself for the A or AAAA records, make a direct IP request and set the host:
header. Is there a better way?
推荐答案
您可以使用 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");
};
这篇关于如何为HttpWebRequest或WebRequest C#强制使用ipv6或ipv4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!