通过特定网络适配器发送 HttpWebRequest
几天前,我问了一个关于发送的问题 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的本地端点是私有 IP 地址(192.168.50.103 是),您的路由器将转换该地址到不同的公共IP,这是whatsmyip可以看到的地址。
我建议你尝试这个例子:
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:
底层平台可能支持也可能不支持您尝试执行的操作。
谷歌搜索“强/弱主机模型”。
例如,这是对该主题的一个很好的介绍:
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