幾天前,我問了一個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