在我的Win7 PC上,我有两个用于VMWare服务器的虚拟网络适配器。当启用这些适配器时,我的HttpWebRequest超时。我真的应该告诉它要绑定到哪个适配器吗?
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url.AbsoluteUri + "etc.txt");
request.Timeout = 2000;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
return reader.ReadToEnd();
}
}更新
我猜这是个常见的问题。有谁有标准的方法来处理这件事吗?我不能真正提示用户的界面,因为他们是非科技。罗希特的回答是展示如何设置ServicePoint的良好开端。
发布于 2010-03-23 11:37:16
提姆,如果你看到超时,这是因为你的新适配器有路由的URL,他们没有到达目的地。
public delegate IPEndPoint BindIPEndPoint(ServicePoint servicePoint, IPEndPoint remoteEndPoint, int retryCount);你可以把它当作
private IPEndPoint BindIPEndPointCallback(ServicePoint servicePoint,IPEndPoint remoteEndPoint, int retryCount)
{
if(retryCount < 3)
return new IPEndPoint(IPAddress.Parse("192.168.10.60"), 0);
else
return new IPEndPoint(IPAddress.Any, 0);
}还有..。
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.ServicePoint.BindIPEndPointDelegate = new BindIPEndPoint(BindIPEndPointCallback);有关更多详细信息,请参阅http://www.netbrick.net/blog/PermaLink,guid,b9c255d9-74b4-45ab-8fd0-c9a04784655a.aspx。
发布于 2010-03-30 14:00:04
继续从罗汉茨的答案。这对所有的适配器都有用吗?
private IPEndPoint BindIPEndPointCallback(ServicePoint servicePoint, IPEndPoint remoteEndPoint, int retryCount)
{
List<IPEndPoint> endPoints = new List<IPEndPoint>();
foreach (NetworkInterface netinface in NetworkInterface.GetAllNetworkInterfaces())
{
foreach (IPAddressInformation unicast in netinface.GetIPProperties().UnicastAddresses)
{
if(unicast.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
endPoints.Add(new IPEndPoint(unicast.Address, 80));
}
}
if (retryCount > endPoints.Count - 1)
return new IPEndPoint(IPAddress.Any, 80);
else
return endPoints[retryCount];
}https://stackoverflow.com/questions/2499423
复制相似问题