通过特定网络适配器发送 HttpWebRequest

发布于 2024-11-04 19:10:24 字数 1212 浏览 1 评论 0原文

几天前,我问了一个关于发送问题 HttpWebRequest 通过特定的网络适配器,有人告诉我使用 BindIPEndPointCallback。我尝试了这个:

public static IPEndPoint BindIPEndPointCallback(ServicePoint servicePoint, IPEndPoint remoteEndPoint, int retryCount)
{
    List<IPEndPoint> ipep = new List<IPEndPoint>();
    foreach (var i in System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces())
    {
        foreach (var ua in i.GetIPProperties().UnicastAddresses)
            ipep.Add(new IPEndPoint(ua.Address, 0));
    }
    return new IPEndPoint(ipep[1].Address, ipep[1].Port);
}

private void button1_Click(object sender, EventArgs e)
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://whatismyip.com");
    request.ServicePoint.BindIPEndPointDelegate = new BindIPEndPoint(BindIPEndPointCallback);
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    StreamReader sr = new StreamReader(response.GetResponseStream());
    string x = sr.ReadToEnd();
}

但它仍然不起作用。它通过同一网络适配器发送HttpWebRequest。 还有什么我可以尝试的吗?

A couple of days ago I asked a question about sending HttpWebRequest through a specific network adapter and someone told me to use BindIPEndPointCallback. I tried this:

public static IPEndPoint BindIPEndPointCallback(ServicePoint servicePoint, IPEndPoint remoteEndPoint, int retryCount)
{
    List<IPEndPoint> ipep = new List<IPEndPoint>();
    foreach (var i in System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces())
    {
        foreach (var ua in i.GetIPProperties().UnicastAddresses)
            ipep.Add(new IPEndPoint(ua.Address, 0));
    }
    return new IPEndPoint(ipep[1].Address, ipep[1].Port);
}

private void button1_Click(object sender, EventArgs e)
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://whatismyip.com");
    request.ServicePoint.BindIPEndPointDelegate = new BindIPEndPoint(BindIPEndPointCallback);
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    StreamReader sr = new StreamReader(response.GetResponseStream());
    string x = sr.ReadToEnd();
}

But it still doesn't work. It sends the HttpWebRequest through the same network adapter.
Is there anything else I could try?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蒲公英的约定 2024-11-11 19:10:24

如果您的本地端点是私有 IP 地址(192.168.50.103 是),您的路由器将转换该地址到不同的公共IP,这是whatsmyip可以看到的地址。

我建议你尝试这个例子:

using System;
using System.Net;

class Program
{
    public static void Main ()
    {
        // TODO: Put your ip addresses in this list
        var ips = new IPAddress[]
        {
            IPAddress.Parse("10.0.0.3"),
            IPAddress.Parse("192.168.1.7")
        };

        foreach (var ip in ips)
        {
            try
            {
                Console.WriteLine("Request from: " + ip);
                var request = (HttpWebRequest)HttpWebRequest.Create("http://ns1.vianett.no/");
                request.ServicePoint.BindIPEndPointDelegate = delegate
                {
                    return new IPEndPoint(ip, 0);
                };
                var response = (HttpWebResponse)request.GetResponse();
                Console.WriteLine("Actual IP: " + response.GetResponseHeader("X-YourIP"));
                response.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

If your local end point is a private ip address (192.168.50.103 is), your router will translate that address to a different public ip, and this is the address whatsmyip can see.

I suggest your try this example:

using System;
using System.Net;

class Program
{
    public static void Main ()
    {
        // TODO: Put your ip addresses in this list
        var ips = new IPAddress[]
        {
            IPAddress.Parse("10.0.0.3"),
            IPAddress.Parse("192.168.1.7")
        };

        foreach (var ip in ips)
        {
            try
            {
                Console.WriteLine("Request from: " + ip);
                var request = (HttpWebRequest)HttpWebRequest.Create("http://ns1.vianett.no/");
                request.ServicePoint.BindIPEndPointDelegate = delegate
                {
                    return new IPEndPoint(ip, 0);
                };
                var response = (HttpWebResponse)request.GetResponse();
                Console.WriteLine("Actual IP: " + response.GetResponseHeader("X-YourIP"));
                response.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}
度的依靠╰つ 2024-11-11 19:10:24

底层平台可能支持也可能不支持您尝试执行的操作。

谷歌搜索“强/弱主机模型”。

例如,这是对该主题的一个很好的介绍:

http://technet .microsoft.com/en-us/library/2007.09.cableguy.aspx

What you are trying to do may or may not be supported by the underlying platform.

Google for "Strong/Weak host models".

For eg, this is a good introduction to the topic:

http://technet.microsoft.com/en-us/library/2007.09.cableguy.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文