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

发布于 2024-11-04 06:47:33 字数 1173 浏览 2 评论 0原文

我的计算机连接了两个无线网络适配器,每个适配器都连接到不同的网络。我想构建一种代理服务器,我的浏览器将连接到该代理服务器,它将从不同的适配器发送 HTTP 请求,因此网页上的加载时间会更短。 你们知道我如何决定从哪个网络适配器发送 HttpWebRequest 吗?

谢谢:)

更新

我使用了这段代码:

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();
}

但是即使更改了IPEndPoint,我发送的从WhatIsMyIp获得的IP仍然是相同的..有什么帮助吗?

I have two wireless network adapters connected to my computer, each connected to a different network. I would like to build a kind of proxy server that my browser would connect to and it will send HTTP requests each from different adapter so loading time on webpages would be smaller.
Do you guys know how can I decide from which network adapter to send the HttpWebRequest?

Thanks :)

UPDATE

I used this code:

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 even if a change the IPEndPoint I send the IP I get from WhatIsMyIp is still the same.. any help?

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

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

发布评论

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

评论(3

可可 2024-11-11 06:47:34

BindIPEndPointDelegate 很可能就是您想要的这里。它允许您强制特定的本地 IP 作为发送 HttpWebRequest 的端点。

BindIPEndPointDelegate may well be what you're after here. It allows you to force a particular local IP to be the end-point via which the HttpWebRequest is sent.

儭儭莪哋寶赑 2024-11-11 06:47:34

这个例子对我有用:

using System;
using System.Net;

class Program
{
    public static void Main ()
    {
        foreach (var ip in Dns.GetHostAddresses (Dns.GetHostName ())) 
        {
            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 ();
        }
    }
}

This example works for me:

using System;
using System.Net;

class Program
{
    public static void Main ()
    {
        foreach (var ip in Dns.GetHostAddresses (Dns.GetHostName ())) 
        {
            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 ();
        }
    }
}
辞旧 2024-11-11 06:47:34

这是因为 WebRequest 使用 ServicePointManager 并且它缓存用于单个 URI 的实际 ServicePoint。因此,在您的情况下,BindIPEndPointDelegate 仅调用一次,所有后续 CreateRequest 都会重用相同的绑定接口。这是一个更底层的 TcpClient 示例,实际有效:

        foreach (var iface in NetworkInterface.GetAllNetworkInterfaces())
        {
            if (iface.OperationalStatus == OperationalStatus.Up && iface.NetworkInterfaceType != NetworkInterfaceType.Loopback)
            {
                Console.WriteLine("Interface: {0}\t{1}\t{2}", iface.Name, iface.NetworkInterfaceType, iface.OperationalStatus);
                foreach (var ua in iface.GetIPProperties().UnicastAddresses)
                {
                    Console.WriteLine("Address: " + ua.Address);
                    try
                    {
                        using (var client = new TcpClient(new IPEndPoint(ua.Address, 0)))
                        {
                            client.Connect("ns1.vianett.no", 80);
                            var buf = Encoding.UTF8.GetBytes("GET / HTTP/1.1\r\nConnection: close\r\nHost: ns1.vianett.no\r\n\r\n");
                            client.GetStream().Write(buf, 0, buf.Length);
                            var sr = new StreamReader(client.GetStream());
                            var all = sr.ReadToEnd();
                            var match = Regex.Match(all, "(?mi)^X-YourIP: (?'a'.+)$");
                            Console.WriteLine("Your address is " + (match.Success ? match.Groups["a"].Value : all));
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
            }
        }

This is because WebRequest uses ServicePointManager and it caches actual ServicePoint that is been used for single URI. So in your case BindIPEndPointDelegate called only once and all subsequent CreateRequest reuses same binded interface. Here is a little bit more lower level example with TcpClient that actually works:

        foreach (var iface in NetworkInterface.GetAllNetworkInterfaces())
        {
            if (iface.OperationalStatus == OperationalStatus.Up && iface.NetworkInterfaceType != NetworkInterfaceType.Loopback)
            {
                Console.WriteLine("Interface: {0}\t{1}\t{2}", iface.Name, iface.NetworkInterfaceType, iface.OperationalStatus);
                foreach (var ua in iface.GetIPProperties().UnicastAddresses)
                {
                    Console.WriteLine("Address: " + ua.Address);
                    try
                    {
                        using (var client = new TcpClient(new IPEndPoint(ua.Address, 0)))
                        {
                            client.Connect("ns1.vianett.no", 80);
                            var buf = Encoding.UTF8.GetBytes("GET / HTTP/1.1\r\nConnection: close\r\nHost: ns1.vianett.no\r\n\r\n");
                            client.GetStream().Write(buf, 0, buf.Length);
                            var sr = new StreamReader(client.GetStream());
                            var all = sr.ReadToEnd();
                            var match = Regex.Match(all, "(?mi)^X-YourIP: (?'a'.+)$");
                            Console.WriteLine("Your address is " + (match.Success ? match.Groups["a"].Value : all));
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
            }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文