如何使用 WinHttpSendRequest 从本地主机接收数据
How to receive data with WinHttpSendRequest from localhost
我尝试使用 GET 参数接收一些数据。
当我尝试从 webadress 获取数据时,它工作正常。
只是我将网络服务器更改为本地主机,我不会收到数据并收到错误 12175
我必须更改什么,我的代码适用于网络连接和本地主机测试?
当我在浏览器中输入本地主机地址时,我会收到预期的结果。
LPCWSTR server = L"www.airfieldmanager-game.de"; //this works fine
//LPCWSTR server = L"localhost"; //causes Error 12175 at WinHttpSendRequest. Variables hSession, hConnect and cRequest looks fine.
LPCWSTR serverRequest = L"/php/TestCURL.php?data=1¶m=Gesendete Daten!";
// Use WinHttpOpen to obtain a session handle.
hSession = WinHttpOpen(L"WinHTTP Example/1.0",
WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
WINHTTP_NO_PROXY_NAME,
WINHTTP_NO_PROXY_BYPASS, 0);
// Specify an HTTP server.
if (hSession)
hConnect = WinHttpConnect(hSession, server,
INTERNET_DEFAULT_HTTP_PORT, 0);
// Create an HTTP request handle.
if (hConnect)
hRequest = WinHttpOpenRequest(hConnect, L"GET", serverRequest,
NULL, WINHTTP_NO_REFERER,
WINHTTP_DEFAULT_ACCEPT_TYPES,
WINHTTP_FLAG_SECURE);
// Send a request.
if (hRequest)
bResults = WinHttpSendRequest(hRequest,
WINHTTP_NO_ADDITIONAL_HEADERS, 0,
WINHTTP_NO_REQUEST_DATA, 0,
0, 0);
// End the request.
if (bResults)
bResults = WinHttpReceiveResponse(hRequest, NULL);
if (!bResults)
{
int i = GetLastError(); //when using loscalhost i becomes 12175
}
错误 12175
这是 ERROR_WINHTTP_SECURE_FAILURE
。 WinHttpSendRequest
解释此错误何时发生以及如何获取更多相关信息:
One or more errors were found in the Secure Sockets Layer (SSL)
certificate sent by the server. To determine what type of error was
encountered, verify through a
WINHTTP_CALLBACK_STATUS_SECURE_FAILURE
notification in a status
callback function.
因此,对于调试诊断,您至少需要编写 self cb:
void CALLBACK WinHttpStatusCB(
__in HINTERNET hInternet,
__in DWORD_PTR dwContext,
__in DWORD dwInternetStatus,
__in LPVOID lpvStatusInformation,
__in DWORD dwStatusInformationLength
)
{
switch (dwInternetStatus)
{
case WINHTTP_CALLBACK_STATUS_SECURE_FAILURE:
if (lpvStatusInformation && dwStatusInformationLength == sizeof(ULONG))
{
DbgPrint("SECURE_FAILURE:[%x]\n", *(PULONG)lpvStatusInformation);
// ...
}
break;
//...
}
}
并激活它:
WinHttpSetStatusCallback(hSession, WinHttpStatusCB, WINHTTP_CALLBACK_FLAG_ALL_NOTIFICATIONS, 0);
或
WinHttpSetStatusCallback(hSession, WinHttpStatusCB, WINHTTP_CALLBACK_FLAG_SECURE_FAILURE, 0);
用这个你可以找到失败的确切原因 - 几个 WINHTTP_CALLBACK_STATUS_FLAG_*
标志。
If the dwInternetStatus
argument is
WINHTTP_CALLBACK_STATUS_SECURE_FAILURE
, then lpvStatusInformation
points to a DWORD
which is a bitwise-OR combination of one or more
of the following values. read more
然而,从一般的角度我们可以理解 - 使用服务器的 sertificat 中的 CN 必须与请求中使用的站点 dns 名称相同。如果你在这里使用 localhost - 那么服务器证书的 CN 名称也必须是本地主机或者你得到了最小值 WINHTTP_CALLBACK_STATUS_FLAG_CERT_CN_INVALID
:
SSL certificate common name (host name field) is incorrect, for
example, if you entered localhost and the common name on the
certificate says heilpraxis-einssein.de.
如果没有出现此类错误,您可以使用 WinHttpSetOption
with WINHTTP_OPTION_SECURITY_FLAGS
。你需要考虑最小设置 SECURITY_FLAG_IGNORE_CERT_CN_INVALID
Allows an invalid common name in a certificate; that is, the server
name specified by the application does not match the common name in
the certificate. If this flag is set, the application does not receive
a WINHTTP_CALLBACK_STATUS_FLAG_CERT_CN_INVALID
callback.
但可以设置多个标志:
ULONG flags =
SECURITY_FLAG_IGNORE_UNKNOWN_CA|
SECURITY_FLAG_IGNORE_CERT_DATE_INVALID|
SECURITY_FLAG_IGNORE_CERT_CN_INVALID|
SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE;
WinHttpSetOption(hRequest, WINHTTP_OPTION_SECURITY_FLAGS, &flags, sizeof(flags));
我尝试使用 GET 参数接收一些数据。 当我尝试从 webadress 获取数据时,它工作正常。 只是我将网络服务器更改为本地主机,我不会收到数据并收到错误 12175
我必须更改什么,我的代码适用于网络连接和本地主机测试?
当我在浏览器中输入本地主机地址时,我会收到预期的结果。
LPCWSTR server = L"www.airfieldmanager-game.de"; //this works fine
//LPCWSTR server = L"localhost"; //causes Error 12175 at WinHttpSendRequest. Variables hSession, hConnect and cRequest looks fine.
LPCWSTR serverRequest = L"/php/TestCURL.php?data=1¶m=Gesendete Daten!";
// Use WinHttpOpen to obtain a session handle.
hSession = WinHttpOpen(L"WinHTTP Example/1.0",
WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
WINHTTP_NO_PROXY_NAME,
WINHTTP_NO_PROXY_BYPASS, 0);
// Specify an HTTP server.
if (hSession)
hConnect = WinHttpConnect(hSession, server,
INTERNET_DEFAULT_HTTP_PORT, 0);
// Create an HTTP request handle.
if (hConnect)
hRequest = WinHttpOpenRequest(hConnect, L"GET", serverRequest,
NULL, WINHTTP_NO_REFERER,
WINHTTP_DEFAULT_ACCEPT_TYPES,
WINHTTP_FLAG_SECURE);
// Send a request.
if (hRequest)
bResults = WinHttpSendRequest(hRequest,
WINHTTP_NO_ADDITIONAL_HEADERS, 0,
WINHTTP_NO_REQUEST_DATA, 0,
0, 0);
// End the request.
if (bResults)
bResults = WinHttpReceiveResponse(hRequest, NULL);
if (!bResults)
{
int i = GetLastError(); //when using loscalhost i becomes 12175
}
错误 12175
这是 ERROR_WINHTTP_SECURE_FAILURE
。 WinHttpSendRequest
解释此错误何时发生以及如何获取更多相关信息:
One or more errors were found in the Secure Sockets Layer (SSL) certificate sent by the server. To determine what type of error was encountered, verify through a
WINHTTP_CALLBACK_STATUS_SECURE_FAILURE
notification in a status callback function.
因此,对于调试诊断,您至少需要编写 self cb:
void CALLBACK WinHttpStatusCB(
__in HINTERNET hInternet,
__in DWORD_PTR dwContext,
__in DWORD dwInternetStatus,
__in LPVOID lpvStatusInformation,
__in DWORD dwStatusInformationLength
)
{
switch (dwInternetStatus)
{
case WINHTTP_CALLBACK_STATUS_SECURE_FAILURE:
if (lpvStatusInformation && dwStatusInformationLength == sizeof(ULONG))
{
DbgPrint("SECURE_FAILURE:[%x]\n", *(PULONG)lpvStatusInformation);
// ...
}
break;
//...
}
}
并激活它:
WinHttpSetStatusCallback(hSession, WinHttpStatusCB, WINHTTP_CALLBACK_FLAG_ALL_NOTIFICATIONS, 0);
或
WinHttpSetStatusCallback(hSession, WinHttpStatusCB, WINHTTP_CALLBACK_FLAG_SECURE_FAILURE, 0);
用这个你可以找到失败的确切原因 - 几个 WINHTTP_CALLBACK_STATUS_FLAG_*
标志。
If the
dwInternetStatus
argument isWINHTTP_CALLBACK_STATUS_SECURE_FAILURE
, thenlpvStatusInformation
points to aDWORD
which is a bitwise-OR combination of one or more of the following values. read more
然而,从一般的角度我们可以理解 - 使用服务器的 sertificat 中的 CN 必须与请求中使用的站点 dns 名称相同。如果你在这里使用 localhost - 那么服务器证书的 CN 名称也必须是本地主机或者你得到了最小值 WINHTTP_CALLBACK_STATUS_FLAG_CERT_CN_INVALID
:
SSL certificate common name (host name field) is incorrect, for example, if you entered localhost and the common name on the certificate says heilpraxis-einssein.de.
如果没有出现此类错误,您可以使用 WinHttpSetOption
with WINHTTP_OPTION_SECURITY_FLAGS
。你需要考虑最小设置 SECURITY_FLAG_IGNORE_CERT_CN_INVALID
Allows an invalid common name in a certificate; that is, the server name specified by the application does not match the common name in the certificate. If this flag is set, the application does not receive a
WINHTTP_CALLBACK_STATUS_FLAG_CERT_CN_INVALID
callback.
但可以设置多个标志:
ULONG flags =
SECURITY_FLAG_IGNORE_UNKNOWN_CA|
SECURITY_FLAG_IGNORE_CERT_DATE_INVALID|
SECURITY_FLAG_IGNORE_CERT_CN_INVALID|
SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE;
WinHttpSetOption(hRequest, WINHTTP_OPTION_SECURITY_FLAGS, &flags, sizeof(flags));