【问题标题】:Choosing which IP the HTTP request is using when having multiple IPs (.NET)在具有多个 IP (.NET) 时选择 HTTP 请求使用的 IP
【发布时间】:2019-01-01 01:36:04
【问题描述】:

我正在编写一个 .NET 程序,它将在具有多个 IP 地址的计算机上运行。该程序向给定的网址发出 HTTP 请求。我想选择我使用的 IP 地址(这样我就可以确定哪个 IP 地址会出现在其他服务器的日志中)。

建议?

【问题讨论】:

    标签: c# .net vb.net ip


    【解决方案1】:

    我相信您可以通过提供BindIPEndPointDelegate 来强制使用本地端点,该BindIPEndPointDelegate 提供要绑定到的 IP/端口。

    string sendingIp = "192.168.0.1";
    int sendingPort = 5000;
    Uri uri = new Uri("http://google.com");
    HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(uri);
    ServicePoint sp = ServicePointManager.FindServicePoint(uri);
    sp.BindIPEndPointDelegate =
        (servicePoint,remoteEp,retryCount) =>
             {
                 return new IPEndPoint(IPAddress.Parse(sendingIp),sendingPort);
             };
    var data = new StreamReader(wr.GetResponse().GetResponseStream()).ReadToEnd();
    

    此代码无法正确处理处置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-07
      • 1970-01-01
      • 2019-06-08
      • 1970-01-01
      • 2011-01-26
      • 1970-01-01
      • 2013-11-08
      相关资源
      最近更新 更多