2012-09-08 68 views
1

我需要執行腳本來訪問綁定到特定IP地址的網站。我在C#中發現了一些使用BindIpEndPointDelegate類的方法。如何使用特定IP地址將請求發送到使用PowerShell的網站

var req = (HttpWebRequest)WebRequest.Create("http://google.com/"); 
req.ServicePoint.BindIPEndPointDelegate = BindTo; 
using (req.GetResponse()); 

static IPEndPoint BindTo(ServicePoint servicepoint, IPEndPoint remoteendpoint, int retrycount) 
{ 
IPAddress ip = IPAddress.Any; //This is where you specify the network adapter's address 
int port = 0; //This in most cases should stay 0. This when 0 will bind to any port  available. 
return new IPEndPoint(ip, port); 
} 

我不知道如何將該類傳遞給powershell。

感謝您的幫助!

+2

,你說什麼樣的訪問呢?香草GET請求? POST?或者完全不同的東西? – Jesper

+0

其實,我只需要做一個HttpWebRequest來檢查網站狀態。我的問題是,網站背後的負載平衡具有不同的IP地址的網站實例。我想向所有人發送請求。這就是爲什麼我需要BindIPEndPointDelegate來指定每個IP地址和端口。 – user1657052

回答

0

這給一試(未測試),這是PowerShell的等價的C#代碼:

function Bind-IPEndPointCallback([System.Net.IPAddress]$ip,$port) 
{ 
     New-Object -TypeName System.Net.IPEndPoint $ip, $port 
} 

$request = [System.Net.HttpWebRequest]::Create("http://somewhere.com") 
$request.ServicePoint.BindIPEndPointDelegate={ (Bind-IPEndPointCallback -ip 10.10.10.10 -port 80) } 
$request.GetResponse() 
相關問題