覆盖 HTTP 请求中的 IP
寻找一种方法来发出 HTTPwebrequest,或使用浏览器控件或 winhttp 向 URL 发出请求,但将其从 DNS 查找连接到的 IP 地址覆盖为特定地址。
尝试执行类似于 HOSTS 文件的操作,但以编程方式进行,而无需修改此文件。它可以是 C# 或 C+
为什么我需要它,我发送请求的主机有多个 IP,并且它们的域服务器正在跨不同的 IP 进行负载平衡。试图强制请求到特定的IP,但我需要http请求中的主机仍然是原始主机。我需要以编程方式进行此操作,因为每次需要运行此测试时更改主机文件太耗时。
Looking for a way to issue an HTTPwebrequest, or use the browser control, or winhttp to make a request to a URL, but override the IP address it connects to from the DNS lookup to a specific one.
Trying to do something similar to the HOSTS file, but programatically without having to modify this file. It can be C# or C+
Why I need it, the host i am sending the request has multiple IPs, and their Domain servers are doing load balancing accross the different IPs. Trying to force the request to a particular IP, but I need the host in the http request to be still the original host. I need this programatically because changing the host file every time i need to run this test is too time consuming.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你所要做的就是这样:
All you had to do was this:
如果我理解正确的话,您必须使用虚拟主机向 Web 服务器发出 http 请求,但 DNS 尚未设置,因此您必须在 url 中指定 IP 地址,但在主机中发送其他内容: 标题。
如果是这种情况,您也许可以这样做..
在 C# 中使用 WebProxy:
请参阅 Kayode Leonard 的回答 适用于 .NET 4 及更高版本。
如果我的服务器在
67.223.227.171:8888
上运行,但我需要www.
。Host:
标头中的 example.com请参阅此链接
在 C++ 中使用 WinHttp:< /strong>
使用 WinHttp,您可以简单地使用 WinHttpAddRequestHeaders 设置
Host:
标头。因此,如果我的服务器在
67.223.227.171:8888
上运行,但我需要在Host:
中有www.example.com
标头:已编辑: 类名称为
WebProxy
。添加了 C# 示例代码。添加了 C++ 示例代码。If I understand correctly you have to make an http request to a web server using virtualhosts but the DNS isn't setup yet so you have to specify the ip address in the url but send something else in the Host: header.
If that's the case you may be able to do so..
In C# using WebProxy:
See Kayode Leonard's answer for .NET 4 and up.
Here's the code I would use if I have my server running on
67.223.227.171:8888
but I need to havewww.example.com
in theHost:
header.See this link
In C++ using WinHttp:
Using WinHttp you can simply set the
Host:
header with WinHttpAddRequestHeaders.So once again if I have my server running on
67.223.227.171:8888
but I need to havewww.example.com
in theHost:
header:Edited: The class name is
WebProxy
. Added C# sample code. Added C++ sample code.[注意,Kayode Leonard 的回答的进一步内容:在 .Net 4.0 中将 Host 属性添加到请求中,使得此答案过时]
我认为您是说您想要能够覆盖给定主机的 IP 地址,而不更改主机标头。
例如,news.bbc.co.uk 映射到 IP 地址 212.58.226.139,但您希望能够将其映射到另一个 IP 地址,同时仍然显示相同的 news.bbc.co。 uk“Host”http 标头到覆盖的地址。这就是您所说的通过覆盖 HOSTS 文件来实现的效果,这与 Jason 的答案略有不同,因为他不会显示原始的“Host”http 标头。
我不相信你能轻易做到这一点(尽管我即将尝试找出答案!)。当然,您不能执行以下操作:
因为这将失败并显示错误,指出您无法修改“Host”标头。
如果您愿意降低到 HttpWebRequest 以下的级别并在更高的 TCP 级别上进行处理,您可能可以做到这一点,但我不确定您如何在不降低到该级别的情况下实现它。
[编辑]:在尝试了各种重写 HttpWebRequest 和 WebHeaderCollection 的方法后,我很确定不能以这种方式完成。然而,亚历山大·贾斯明的回答似乎是解决方案。
[Note, further to Kayode Leonard's answer: A Host property was added to the request in .Net 4.0, making this answer obsolete]
I think you are saying that you want to be able to override the ip address for a given host, without changing the host header.
For example, news.bbc.co.uk maps to IP address 212.58.226.139, but you want to be able to map this to another ip address, while still presenting the same news.bbc.co.uk "Host" http header to the overriden address. This is what you'd acheive by overriding the HOSTS file as you say, which is slightly different to Jason's answer as his won't present the original "Host" http header.
I don't believe you can do this easily (although I'm about to experiment to find out!). Certainly you can't do the following:
as this will fail with an error saying you can't modify the "Host" header.
You probably can do it if your are willing to go down a level below the HttpWebRequest and deal at a more TCP level, but I'm not sure how you'd approach it without going down to that level.
[Edit]: Having played around with various approaches of overriding HttpWebRequest and WebHeaderCollection, I'm pretty sure it can't be done this way. However, Alexandre Jasmin's answer seems to be the solution.
我添加了一个依赖于
curl
和--resolve
命令行选项的答案。https://curl.haxx.se/docs/manpage.html#--解析
从高级语言中,通常可以调用
curl
。I'm adding an answer that relies on
curl
and the--resolve
command line option.https://curl.haxx.se/docs/manpage.html#--resolve
From higher level languages, it's usually possible to invoke
curl
.