2

我需要执行一个脚本来访问绑定到特定 IP 地址的网站。我在 C# 中找到了使用 BindIpEndPointDelegate 类来做到这一点的东西。

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。

感谢所有的帮助!

4

1 回答 1

1

试一试(未测试),这是 C# 代码的 PowerShell 等效项:

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()
于 2012-09-08T19:25:26.547 回答