2011-05-02 15 views
9

Bilgisayarıma bağlı, her biri farklı bir ağa bağlı iki kablosuz ağ bağdaştırıcım var. Tarayıcımın bağlayacağı bir çeşit proxy sunucu oluşturmak istiyorum ve her biri farklı adaptörlerden HTTP istekleri gönderecek, böylece web sayfalarında yükleme süresi daha küçük olacaktır. HttpWebRequest'i hangi ağ bağdaştırıcısından göndereceğime nasıl karar verebilirim?HttpWebRequest ürününü belirli bir ağ bağdaştırıcısı üzerinden gönderme

Teşekkür :)

GÜNCELLEME

Bu kodu kullandı:

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(); 
} 

Ama WhatIsMyIp aldığım IPEndPoint ben IP göndermek bir değişiklik hala aynı olsa bile. . herhangi bir yardım?

+0

Bir proxy üzerinden mi bağlanıyorsunuz? –

cevap

6

BindIPEndPointDelegate, burada olduğun şey olabilir. Belirli bir yerel IP'yi, HttpWebRequest öğesinin gönderildiği bitiş noktası olmasını zorlamanıza olanak tanır.

+0

@Ephi - sorun değil, yardımcı olabilirim. Bundan memnun olduğunuzu düşünüyorsanız lütfen cevabımı kabul edin. –

+0

@ Will-A Sorumu düzenledim, bana yardımcı olabilir misiniz? – Ephi

+0

Evet, 'HttpWebRequest' üzerindeki 'ServicePoint' özelliği aracılığıyla 'BindIPEndPointDelegate' özelliğini kullanarak, hangi bağdaştırıcının isteği göndermesini sağlayabilirsiniz. Bu soruya verilen yanıtın bir örneği var (http://stackoverflow.com/questions/3345387/how-to-change-originating-ip-in-httpwebrequest). – rsbarro

2

Bu örnek benim için çalışıyor: WebRequest ServicePointManager kullanır ve tek URI için kullanılan be fiili ServicePoint önbelleğe aldığından

using System; 
using System.Net; 

class Program 
{ 
    public static void Main() 
    { 
     foreach (var ip in Dns.GetHostAddresses (Dns.GetHostName())) 
     { 
      Console.WriteLine ("Request from: " + ip); 
      var request = (HttpWebRequest)HttpWebRequest.Create ("http://ns1.vianett.no/"); 
      request.ServicePoint.BindIPEndPointDelegate = delegate { 
       return new IPEndPoint (ip, 0); 
      }; 
      var response = (HttpWebResponse)request.GetResponse(); 
      Console.WriteLine ("Actual IP: " + response.GetResponseHeader ("X-YourIP")); 
      response.Close(); 
     } 
    } 
} 
0

budur. Dolayısıyla, durumunuzda BindIPEndPointDelegate yalnızca bir kez çağrıldı ve sonraki tüm CreateRequest aynı bağlayıcı arabirimi yeniden kullanıyor. Burada aslında çalışan daha küçük bir örnek TcpClient ile çalışıyor:

 foreach (var iface in NetworkInterface.GetAllNetworkInterfaces()) 
     { 
      if (iface.OperationalStatus == OperationalStatus.Up && iface.NetworkInterfaceType != NetworkInterfaceType.Loopback) 
      { 
       Console.WriteLine("Interface: {0}\t{1}\t{2}", iface.Name, iface.NetworkInterfaceType, iface.OperationalStatus); 
       foreach (var ua in iface.GetIPProperties().UnicastAddresses) 
       { 
        Console.WriteLine("Address: " + ua.Address); 
        try 
        { 
         using (var client = new TcpClient(new IPEndPoint(ua.Address, 0))) 
         { 
          client.Connect("ns1.vianett.no", 80); 
          var buf = Encoding.UTF8.GetBytes("GET/HTTP/1.1\r\nConnection: close\r\nHost: ns1.vianett.no\r\n\r\n"); 
          client.GetStream().Write(buf, 0, buf.Length); 
          var sr = new StreamReader(client.GetStream()); 
          var all = sr.ReadToEnd(); 
          var match = Regex.Match(all, "(?mi)^X-YourIP: (?'a'.+)$"); 
          Console.WriteLine("Your address is " + (match.Success ? match.Groups["a"].Value : all)); 
         } 
        } 
        catch (Exception ex) 
        { 
         Console.WriteLine(ex.Message); 
        } 
       } 
      } 
     } 
İlgili konular