如果我理解正确,您必须使用虚拟主机向 Web 服务器发出 http 请求,但 DNS 尚未设置,因此您必须在 url 中指定 ip 地址,但在 Host 中发送其他内容: 标题。
如果是这样的话,你也许可以这样做..
在 C# 中使用 WebProxy:
请参阅 Kayode Leonard's answer 了解 .NET 4 及更高版本。
如果我的服务器在 67.223.227.171:8888 上运行,我将使用以下代码,但我需要在 Host: 标头中包含 www.example.com。
System.Net.WebRequest r = System.Net.WebRequest.Create("http://www.example.com");
r.Proxy = new WebProxy("http://67.223.227.171:8888");
见this link
在 C++ 中使用 WinHttp:
使用 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++ 示例代码。