如何更改HttpWebRequest的原始IP [英] how to change originating IP in HttpWebRequest
问题描述
我运行已分配IP地址5的服务器上此应用程序。我使用的HttpWebRequest从网站获取的一些数据。但是,当我进行连接我可以指定其中5 IP地址之一,以从连接。是否HttpWebRequest的支持呢?如果没有我可以从它继承一个类来改变它的行为?我需要这里这么的想法。
我的code现在的问题是这样的:
System.Net.WebRequest请求= System.Net.WebRequest.Create(链接);
((HttpWebRequest的)要求).Referer =http://application.com;
使用(System.Net.WebResponse响应= request.GetResponse())
{
StreamReader的SR =新的StreamReader(response.GetResponseStream());
返回sr.ReadToEnd();
}
据<一个href=\"http://stackoverflow.com/questions/2829238/how-to-change-the-request-ip-in-httpwebrequest\">this,没有。您可能要下降到使用套接字,在那里我知道你可以选择本地IP。
编辑:实际上,它似乎是可能的。 HttpWebRequest的拥有的ServicePoint属性,这反过来又<一个href=\"http://msdn.microsoft.com/en-us/library/system.net.servicepoint.bindipendpointdelegate.aspx\">BindIPEndPointDelegate,这可能是你在找什么。
给我一分钟,我会掀起一个例子...
HttpWebRequest的REQ =(HttpWebRequest的)WebRequest.Create(http://stackoverflow.com);req.ServicePoint.BindIPEndPointDelegate =委托(
的ServicePoint服务点,
IPEndPoint remoteEndPoint,
INT RetryCount重){ 如果(remoteEndPoint.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6){
返回新IPEndPoint(IPAddress.IPv6Any,0);
}其他{
返回新IPEndPoint(IPAddress.Any,0);
}};Console.WriteLine(req.GetResponse()ResponseUri);
基本上,委托必须返回一个IPEndPoint。你可以选择任何你想要的,但如果它不能绑定到它,它会再打电话委托,达int.MAX_VALUE的时间。这就是为什么我包括code处理IPv6,因为IPAddress.Any是IPv4的。
如果你不关心IPv6,可以摆脱这一点。另外,我离开了ip地址的实际选择作为练习读者:)
I'm running this application on a server that has assigned 5 IPs. I use HttpWebRequest to fetch some data from a website. But when I make the connection I have be able to specify which one of the 5 IPs to make the connection from. Does HttpWebRequest support this? If it doesn't can I inherit a class from it to change it's behavior? I need so ideas here.
My code right now is something like:
System.Net.WebRequest request = System.Net.WebRequest.Create(link);
((HttpWebRequest)request).Referer = "http://application.com";
using (System.Net.WebResponse response = request.GetResponse())
{
StreamReader sr = new StreamReader(response.GetResponseStream());
return sr.ReadToEnd();
}
According to this, no. You may have to drop down to using Sockets, where I know you can choose the local IP.
EDIT: actually, it seems that it may be possible. HttpWebRequest has a ServicePoint Property, which in turn has BindIPEndPointDelegate, which may be what you're looking for.
Give me a minute, I'm going to whip up an example...
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);
Basically, the delegate has to return an IPEndPoint. You can pick whatever you want, but if it can't bind to it, it'll call the delegate again, up to int.MAX_VALUE times. That's why I included code to handle IPv6, since IPAddress.Any is IPv4.
If you don't care about IPv6, you can get rid of that. Also, I leave the actual choosing of the IPAddress as an exercise to the reader :)
这篇关于如何更改HttpWebRequest的原始IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!