通过特定网络适配器发送 HttpWebRequest
我的计算机连接了两个无线网络适配器,每个适配器都连接到不同的网络。我想构建一种代理服务器,我的浏览器将连接到该代理服务器,它将从不同的适配器发送 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
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.
这个例子对我有用:
This example works for me:
这是因为 WebRequest 使用 ServicePointManager 并且它缓存用于单个 URI 的实际 ServicePoint。因此,在您的情况下,BindIPEndPointDelegate 仅调用一次,所有后续 CreateRequest 都会重用相同的绑定接口。这是一个更底层的 TcpClient 示例,实际有效:
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: