【问题标题】:How to send httpwebrequests through specific ip address如何通过特定的 IP 地址发送 httpwebrequests
【发布时间】:2012-07-08 05:17:03
【问题描述】:

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

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

【问题讨论】:

标签: c# httpwebrequest ip


【解决方案1】:

取自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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-16
    • 2014-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-12
    • 2015-10-18
    相关资源
    最近更新 更多