【发布时间】:2011-05-03 13:28:10
【问题描述】:
几天前,我向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。
还有什么我可以尝试的吗?
【问题讨论】:
-
回调是否被调用?它返回什么?
-
是的,它被调用并返回一个 IPEndPoint 对象 - 192.168.50.103:0
标签: c# httpwebrequest