在有多个 IP (.NET) 时选择 HTTP 请求使用的 IP [英] Choosing which IP the HTTP request is using when having multiple IPs (.NET)
本文介绍了在有多个 IP (.NET) 时选择 HTTP 请求使用的 IP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在编写一个 .NET 程序,它将在具有多个 IP 地址的计算机上运行.该程序向给定的网址发出 HTTP 请求.我想选择我使用的 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();
此代码未正确处理处置.
This code doesn't deal with disposal correctly.
这篇关于在有多个 IP (.NET) 时选择 HTTP 请求使用的 IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文