选择具有多个IP地址时,使用该IP的HTTP请求(.NET) [英] Choosing which IP the HTTP request is using when having multiple IPs (.NET)

查看:366
本文介绍了选择具有多个IP地址时,使用该IP的HTTP请求(.NET)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个.NET程序,该程序将运行在计算机上使用多个IP地址。该程序使HTTP请求到指定的Web地址。我想选择哪个IP地址我使用(这样我就可以决定哪些IP地址将出现在其他服务器的日志)。

I am writing a .NET program which will run on a computer with several IP addresses. The program makes HTTP requests to given web addresses. I want to choose which IP address I use (so I can determine which IP address will appear on the log of the other server).

建议?

推荐答案

我相信你可以通过提供强制本地端点的 BindIPEndPointDelegate 它提供的IP /端口绑定到。

I believe you can force the local endpoint by providing a BindIPEndPointDelegate which supplies the IP/port to bind to.

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();

这code不处理处置正确。

This code doesn't deal with disposal correctly.

这篇关于选择具有多个IP地址时,使用该IP的HTTP请求(.NET)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆