2010-03-23 58 views
2

在我的Win7 PC上,我有幾個用於VMWare服務器的虛擬網絡適配器。 啓用這些適配器時,我的HttpWebRequest超時。我真的應該告訴它綁定哪個適配器嗎?啓用多個網絡適配器後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(); 
       } 
      } 

UPDATE

我猜這是一個常見的問題。有沒有人有一個標準的方式來處理這個問題?我不能真正提示用戶接口,因爲他們是非技術人員。 Rohit的答案是展示如何設置ServicePoint的良好開端。

回答

4

Tim,如果您看到超時,那是因爲您的新適配器具有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

0

繼續從Rohits回答。在嘗試使用所有適配器的情況下,這樣做會很好嗎

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]; 
    }