几天前,我问了一个question关于通过特定的网络适配器发送HttpWebRequest
,有人告诉我使用BindIPEndPointCallback
。我试过这个:通过特定的网络适配器发送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();
}
但它仍然不起作用。它通过相同的网络适配器发送HttpWebRequest
。 还有什么我可以尝试吗?
是回调invokd?它返回什么? – jgauffin 2011-05-03 13:42:58
是的,它被调用并返回一个IPEndPoint对象 - 192.168.50.103:0 – Ephi 2011-05-03 14:20:03