几天前,我问question有关通过特定网络适配器发送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
。
还有什么我可以尝试的吗?
答案 0 :(得分:0)
您尝试做的事情可能会或可能不会受到底层平台的支持。
谷歌的“强/弱主机型号”。
例如,这是对该主题的一个很好的介绍:
http://technet.microsoft.com/en-us/library/2007.09.cableguy.aspx
答案 1 :(得分:0)
如果您的本地端点是private ip address(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);
}
}
}
}