覆盖 HTTP 请求中的 IP

发布于 2024-08-17 04:10:44 字数 278 浏览 4 评论 0原文

寻找一种方法来发出 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

多像笑话 2024-08-24 04:10:44

你所要做的就是这样:

var request = (HttpWebRequest) WebRequest.Create("http://192.168.1.1");
request.Host = "news.bbc.co.uk";

All you had to do was this:

var request = (HttpWebRequest) WebRequest.Create("http://192.168.1.1");
request.Host = "news.bbc.co.uk";
远山浅 2024-08-24 04:10:44

如果我理解正确的话,您必须使用虚拟主机向 Web 服务器发出 http 请求,但 DNS 尚未设置,因此您必须在 url 中指定 IP 地址,但在主机中发送其他内容: 标题。

如果是这种情况,您也许可以这样做..

在 C# 中使用 WebProxy:

请参阅 Kayode Leonard 的回答 适用于 .NET 4 及更高版本。

如果我的服务器在 67.223.227.171:8888 上运行,但我需要 www. Host: 标头中的 example.com

System.Net.WebRequest r = System.Net.WebRequest.Create("http://www.example.com");
r.Proxy = new WebProxy("http://67.223.227.171:8888");

请参阅此链接

在 C++ 中使用 WinHttp:< /strong>

使用 WinHttp,您可以简单地使用 WinHttpAddRequestHeaders 设置 Host: 标头。

因此,如果我的服务器在 67.223.227.171:8888 上运行,但我需要在 Host: 中有 www.example.com标头:

#include <windows.h>
#include <winhttp.h>
#include <assert.h>

int main() {
  HINTERNET hSession = WinHttpOpen(L"A WinHTTP Example Program/1.0", 
                                    WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
                                    WINHTTP_NO_PROXY_NAME, 
                                    WINHTTP_NO_PROXY_BYPASS, 0);

  assert(hSession != NULL);


  // Use WinHttpConnect to specify an HTTP server.
  HINTERNET hConnect = WinHttpConnect( hSession,
                             L"67.223.227.171",
                             8888,
                             0 );

  assert(hConnect != NULL);

  // Open and Send a Request Header.
  HINTERNET  hRequest = WinHttpOpenRequest( hConnect,
                                 L"GET", 
                                 L"/downloads/samples/internet/winhttp/retoptions/redirect.asp", 
                                 NULL,
                                 WINHTTP_NO_REFERER,
                                 WINHTTP_DEFAULT_ACCEPT_TYPES,
                                 0 );

    assert(hRequest != NULL);

   BOOL httpResult = WinHttpAddRequestHeaders(
                                  hRequest,
                                  L"Host: www.example.com",
                                  -1L,
                                  0);

   assert(httpResult);

  httpResult = WinHttpSendRequest( hRequest,
                                   WINHTTP_NO_ADDITIONAL_HEADERS, 
                                   0,
                                   WINHTTP_NO_REQUEST_DATA,
                                   0,
                                   0,
                                   0 );

  assert(httpResult);

  httpResult = WinHttpReceiveResponse( hRequest, NULL );

  assert(httpResult);
}

已编辑: 类名称为 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 have www.example.com in the Host: header.

System.Net.WebRequest r = System.Net.WebRequest.Create("http://www.example.com");
r.Proxy = new WebProxy("http://67.223.227.171:8888");

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 have www.example.com in the Host: header:

#include <windows.h>
#include <winhttp.h>
#include <assert.h>

int main() {
  HINTERNET hSession = WinHttpOpen(L"A WinHTTP Example Program/1.0", 
                                    WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
                                    WINHTTP_NO_PROXY_NAME, 
                                    WINHTTP_NO_PROXY_BYPASS, 0);

  assert(hSession != NULL);


  // Use WinHttpConnect to specify an HTTP server.
  HINTERNET hConnect = WinHttpConnect( hSession,
                             L"67.223.227.171",
                             8888,
                             0 );

  assert(hConnect != NULL);

  // Open and Send a Request Header.
  HINTERNET  hRequest = WinHttpOpenRequest( hConnect,
                                 L"GET", 
                                 L"/downloads/samples/internet/winhttp/retoptions/redirect.asp", 
                                 NULL,
                                 WINHTTP_NO_REFERER,
                                 WINHTTP_DEFAULT_ACCEPT_TYPES,
                                 0 );

    assert(hRequest != NULL);

   BOOL httpResult = WinHttpAddRequestHeaders(
                                  hRequest,
                                  L"Host: www.example.com",
                                  -1L,
                                  0);

   assert(httpResult);

  httpResult = WinHttpSendRequest( hRequest,
                                   WINHTTP_NO_ADDITIONAL_HEADERS, 
                                   0,
                                   WINHTTP_NO_REQUEST_DATA,
                                   0,
                                   0,
                                   0 );

  assert(httpResult);

  httpResult = WinHttpReceiveResponse( hRequest, NULL );

  assert(httpResult);
}

Edited: The class name is WebProxy. Added C# sample code. Added C++ sample code.

自此以后,行同陌路 2024-08-24 04:10:44

[注意,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 标头。

我不相信你能轻易做到这一点(尽管我即将尝试找出答案!)。当然,您不能执行以下操作:

var request = (HttpWebRequest) WebRequest.Create("http://192.168.1.1");
request.Headers["Host"] = "news.bbc.co.uk";

因为这将失败并显示错误,指出您无法修改“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:

var request = (HttpWebRequest) WebRequest.Create("http://192.168.1.1");
request.Headers["Host"] = "news.bbc.co.uk";

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.

终遇你 2024-08-24 04:10:44

我添加了一个依赖于 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文