如何通过特定的IP地址发送httpwebrequests

时间:2012-07-08 15:36:25

标签: c# httpwebrequest ip

昨天,我收到了邮件,宣布微小的亚马逊EC2实例最多可以拥有8个IP地址。

让我们说,我正在其中一个实例上运行一个应用程序。我正在使用httpwebrequest来访问网站。我如何控制请求来自哪个IP?

1 个答案:

答案 0 :(得分:2)

取自this question

HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://stackoverflow.com");

req.ServicePoint.BindIPEndPointDelegate = delegate(
ServicePoint servicePoint,
IPEndPoint remoteEndPoint,
int retryCount) {

if (remoteEndPoint.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6) {
    return new IPEndPoint(IPAddress.IPv6Any, 0);
} else {
    return new IPEndPoint(IPAddress.Any, 0);
}

};

Console.WriteLine(req.GetResponse().ResponseUri);