调用 WinHttpOpenRequest 后 GetLastError returns 6
GetLastError returns 6 after call to WinHttpOpenRequest
我不确定这是否是 msdn 知识不足。我有以下代码:
#include "stdafx.h"
#include <iostream>
#include <string>
#include <windows.h>
#include <WinHttp.h>
#include "myHTTP.h"
int main(){
WinHTTP newHTTP("test", "test");
bool myResult;
newHTTP.httpConnect(L"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36",
L"https://pages.awscloud.com/awsomedayonlineconference-reg.html",
1,
L"GET");
newHTTP.httpAddHeader(L"Content-Type: application/x-www-form-urlencoded\r\n");
newHTTP.httpSend();
myResult = newHTTP.httpReceive();
newHTTP.closeHandles();
return 0;
}
我有一个 class 并且正在执行以下行:
// 打开请求 - 此时未连接
hRequest = WinHttpOpenRequest(hConnect, protocol.c_str(), NULL, NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, 0);
if (!hRequest) {
printf("error2: %d", GetLastError());
}
所以基本上当我 运行 我的软件时,它 returns 这里是因为调用 winhttpopenrequest 时 !hrequest 是空的。我添加了 getlasterror 以查看这是为什么,但我得到的唯一输出是:
error2: 6
然后我阅读了 winhttp 的 msdn,我可以在这里看到函数返回的错误:https://msdn.microsoft.com/en-us/library/windows/desktop/aa384099(v=vs.85).aspx 但是这个神秘的错误 6 对我来说毫无意义。
任何关于句柄无效原因的帮助?
完整 class 代码:
#pragma once
// WinHTTP wrapper for web protocol -- windows 8.1+
class WinHTTP {
private:
std::string siteUsername, sitePassword;
std::wstring UA, URL;
bool bResult = false;
DWORD dwSize = sizeof(DWORD); // used to handle reading data in bytes
LPSTR pszOutBuffer; // used to Allocate space for the buffer.
DWORD dwDownloaded = 0; // set to null if using asynchronously and use in callback function only
HINTERNET hSession = NULL, hConnect = NULL, hRequest = NULL;
public:
WinHTTP(std::string myuser = "", std::string mypass = "") {
siteUsername = myuser;
sitePassword = mypass;
}
// TODO: update to be able to add proxy details either here or before. do check if proxy has been detected in here and open/connect accordingly
void httpConnect(std::wstring userAgent, std::wstring myURL, int isHTTPS, std::wstring protocol) {
UA = userAgent;
URL = myURL;
std::wstring acceptTypes = L"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8";
int portToUse;
if (isHTTPS == 1) {
portToUse = 443;
}
else {
portToUse = 80;
}
//initialize http and return session handle -- use c_str to convert wstring to LPCWSTR
hSession = WinHttpOpen(UA.c_str(),
WINHTTP_ACCESS_TYPE_NO_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
//make the connection request
if (hSession) {
hConnect = WinHttpConnect(hSession, URL.c_str(), portToUse, 0);
}
else {
std::cout << "winhttpconnect error " << GetLastErrorAsString();
}
// open the request - not connected at this point
hRequest = WinHttpOpenRequest(hConnect, protocol.c_str(), NULL, NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, 0);
if (!hRequest) {
std::cout << "winhttpopenrequest error " << GetLastErrorAsString();
}
}
std::string GetLastErrorAsString()
{
//Get the error message, if any.
DWORD errorMessageID = ::GetLastError();
if (errorMessageID == 0)
return std::string(); //No error message has been recorded
LPSTR messageBuffer = nullptr;
size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&messageBuffer, 0, NULL);
std::string message(messageBuffer, size);
//Free the buffer.
LocalFree(messageBuffer);
return message;
}
void httpAddHeader(std::wstring myheader) {
if (hRequest) {
bResult = WinHttpAddRequestHeaders(hRequest, myheader.c_str(), (ULONG)-1L, WINHTTP_ADDREQ_FLAG_ADD);
}
}
bool httpSend() {
if (hRequest) {
bResult = WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0);
}
if (!bResult) {
std::cout << "winhttpsendrequest error " << GetLastErrorAsString();
return false;
}
else {
return true;
}
}
bool httpReceive() {
if (bResult) {
bResult = WinHttpReceiveResponse(hRequest, NULL);
}
if (bResult) {
do
{
// Check for available data.
dwSize = 0; //query data available looks for data in bytes
if (!WinHttpQueryDataAvailable(hRequest, &dwSize))
{
std::cout << "WinHttpQueryDataAvailable error " << GetLastErrorAsString();
break;
}
// No more available data.
if (!dwSize)
return false;
// Allocate space for the buffer. as dwSize now holds the size of the request
pszOutBuffer = new char[dwSize + 1]; // just a way of freeing up memory
if (!pszOutBuffer)
{
printf("Out of memory\n"); // couldnt allocate enough
return false;
}
ZeroMemory(pszOutBuffer, dwSize + 1); // fills a block of memory with 0s
// we know the expect size and have the pszoutbffer to write to - read the Data.
if (!WinHttpReadData(hRequest, (LPVOID)pszOutBuffer,
dwSize, &dwDownloaded))
{
std::cout << "WinHttpReadData error " << GetLastErrorAsString();
return false;
}
else
{
printf("%s", pszOutBuffer);
}
// Free the memory allocated to the buffer.
delete[] pszOutBuffer;
return true;
// This condition should never be reached since WinHttpQueryDataAvailable
// reported that there are bits to read.
if (!dwDownloaded)
return false;
} while (dwSize > 0);
}
return false;
}
void closeHandles() {
if (hRequest) WinHttpCloseHandle(hRequest);
if (hConnect) WinHttpCloseHandle(hConnect);
if (hSession) WinHttpCloseHandle(hSession);
}
};
为什么会收到错误代码 6
错误代码 6 是 System Error Code ERROR_INVALID_HANDLE
.
告诉你你传给WinHttpOpenRequest
的hConnect
是无效的
您只在调用 WinHttpOpen
后检查 hSession
,在调用 WinHttpOpenRequest
后检查 hRequest
。但是你永远不会检查 hConnect
是否有效。
您还需要检查 WinHttpConnect
返回的 hConnect
,如果它无效,请在调用另一个 WINAPI 方法之前检查 GetLastError()
:
hSession = WinHttpOpen(UA.c_str(), WINHTTP_ACCESS_TYPE_NO_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
if (hSession) {
hConnect = WinHttpConnect(hSession, URL.c_str(), portToUse, 0);
if (hConnect) {
hRequest = WinHttpOpenRequest(hConnect, protocol.c_str(), NULL, NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, 0);
if (!hRequest) {
std::cout << "WinHttpOpenRequest error " << GetLastErrorAsString();
}
}
else {
std::cout << "WinHttpConnect error " << GetLastErrorAsString();
}
}
else {
std::cout << "WinHttpOpen error " << GetLastErrorAsString();
}
为什么你对 WinHttpConnect
的调用失败
docs 告诉我们 WinHttpConnect
需要 服务器名称 ,而不是 URL:
pswzServerName [in]
Pointer to a null-terminated string that contains the host name of an
HTTP server. Alternately, the string can contain the IP address of the
site in ASCII, for example, 10.0.1.45.
但是您提供的字符串包含 URL,这是无效的。您需要将 "https://pages.awscloud.com/awsomedayonlineconference-reg.html"
更改为 "pages.awscloud.com"
,然后在 WinHttpOpenRequest
中提供页面路径作为参数:
hRequest = WinHttpOpenRequest(hConnect, protocol.c_str(), path.c_str(), NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, 0);
其中 path
是一个包含 "/awsomedayonlineconference-reg.html"
.
的字符串
为此,您可以将 URL 拆分成多个部分,或者更改 httpConnect
方法以将服务器名和路径作为单独的参数。
我不确定这是否是 msdn 知识不足。我有以下代码:
#include "stdafx.h"
#include <iostream>
#include <string>
#include <windows.h>
#include <WinHttp.h>
#include "myHTTP.h"
int main(){
WinHTTP newHTTP("test", "test");
bool myResult;
newHTTP.httpConnect(L"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36",
L"https://pages.awscloud.com/awsomedayonlineconference-reg.html",
1,
L"GET");
newHTTP.httpAddHeader(L"Content-Type: application/x-www-form-urlencoded\r\n");
newHTTP.httpSend();
myResult = newHTTP.httpReceive();
newHTTP.closeHandles();
return 0;
}
我有一个 class 并且正在执行以下行: // 打开请求 - 此时未连接
hRequest = WinHttpOpenRequest(hConnect, protocol.c_str(), NULL, NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, 0);
if (!hRequest) {
printf("error2: %d", GetLastError());
}
所以基本上当我 运行 我的软件时,它 returns 这里是因为调用 winhttpopenrequest 时 !hrequest 是空的。我添加了 getlasterror 以查看这是为什么,但我得到的唯一输出是:
error2: 6
然后我阅读了 winhttp 的 msdn,我可以在这里看到函数返回的错误:https://msdn.microsoft.com/en-us/library/windows/desktop/aa384099(v=vs.85).aspx 但是这个神秘的错误 6 对我来说毫无意义。
任何关于句柄无效原因的帮助?
完整 class 代码:
#pragma once
// WinHTTP wrapper for web protocol -- windows 8.1+
class WinHTTP {
private:
std::string siteUsername, sitePassword;
std::wstring UA, URL;
bool bResult = false;
DWORD dwSize = sizeof(DWORD); // used to handle reading data in bytes
LPSTR pszOutBuffer; // used to Allocate space for the buffer.
DWORD dwDownloaded = 0; // set to null if using asynchronously and use in callback function only
HINTERNET hSession = NULL, hConnect = NULL, hRequest = NULL;
public:
WinHTTP(std::string myuser = "", std::string mypass = "") {
siteUsername = myuser;
sitePassword = mypass;
}
// TODO: update to be able to add proxy details either here or before. do check if proxy has been detected in here and open/connect accordingly
void httpConnect(std::wstring userAgent, std::wstring myURL, int isHTTPS, std::wstring protocol) {
UA = userAgent;
URL = myURL;
std::wstring acceptTypes = L"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8";
int portToUse;
if (isHTTPS == 1) {
portToUse = 443;
}
else {
portToUse = 80;
}
//initialize http and return session handle -- use c_str to convert wstring to LPCWSTR
hSession = WinHttpOpen(UA.c_str(),
WINHTTP_ACCESS_TYPE_NO_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
//make the connection request
if (hSession) {
hConnect = WinHttpConnect(hSession, URL.c_str(), portToUse, 0);
}
else {
std::cout << "winhttpconnect error " << GetLastErrorAsString();
}
// open the request - not connected at this point
hRequest = WinHttpOpenRequest(hConnect, protocol.c_str(), NULL, NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, 0);
if (!hRequest) {
std::cout << "winhttpopenrequest error " << GetLastErrorAsString();
}
}
std::string GetLastErrorAsString()
{
//Get the error message, if any.
DWORD errorMessageID = ::GetLastError();
if (errorMessageID == 0)
return std::string(); //No error message has been recorded
LPSTR messageBuffer = nullptr;
size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&messageBuffer, 0, NULL);
std::string message(messageBuffer, size);
//Free the buffer.
LocalFree(messageBuffer);
return message;
}
void httpAddHeader(std::wstring myheader) {
if (hRequest) {
bResult = WinHttpAddRequestHeaders(hRequest, myheader.c_str(), (ULONG)-1L, WINHTTP_ADDREQ_FLAG_ADD);
}
}
bool httpSend() {
if (hRequest) {
bResult = WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0);
}
if (!bResult) {
std::cout << "winhttpsendrequest error " << GetLastErrorAsString();
return false;
}
else {
return true;
}
}
bool httpReceive() {
if (bResult) {
bResult = WinHttpReceiveResponse(hRequest, NULL);
}
if (bResult) {
do
{
// Check for available data.
dwSize = 0; //query data available looks for data in bytes
if (!WinHttpQueryDataAvailable(hRequest, &dwSize))
{
std::cout << "WinHttpQueryDataAvailable error " << GetLastErrorAsString();
break;
}
// No more available data.
if (!dwSize)
return false;
// Allocate space for the buffer. as dwSize now holds the size of the request
pszOutBuffer = new char[dwSize + 1]; // just a way of freeing up memory
if (!pszOutBuffer)
{
printf("Out of memory\n"); // couldnt allocate enough
return false;
}
ZeroMemory(pszOutBuffer, dwSize + 1); // fills a block of memory with 0s
// we know the expect size and have the pszoutbffer to write to - read the Data.
if (!WinHttpReadData(hRequest, (LPVOID)pszOutBuffer,
dwSize, &dwDownloaded))
{
std::cout << "WinHttpReadData error " << GetLastErrorAsString();
return false;
}
else
{
printf("%s", pszOutBuffer);
}
// Free the memory allocated to the buffer.
delete[] pszOutBuffer;
return true;
// This condition should never be reached since WinHttpQueryDataAvailable
// reported that there are bits to read.
if (!dwDownloaded)
return false;
} while (dwSize > 0);
}
return false;
}
void closeHandles() {
if (hRequest) WinHttpCloseHandle(hRequest);
if (hConnect) WinHttpCloseHandle(hConnect);
if (hSession) WinHttpCloseHandle(hSession);
}
};
为什么会收到错误代码 6
错误代码 6 是 System Error Code ERROR_INVALID_HANDLE
.
告诉你你传给WinHttpOpenRequest
的hConnect
是无效的
您只在调用 WinHttpOpen
后检查 hSession
,在调用 WinHttpOpenRequest
后检查 hRequest
。但是你永远不会检查 hConnect
是否有效。
您还需要检查 WinHttpConnect
返回的 hConnect
,如果它无效,请在调用另一个 WINAPI 方法之前检查 GetLastError()
:
hSession = WinHttpOpen(UA.c_str(), WINHTTP_ACCESS_TYPE_NO_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
if (hSession) {
hConnect = WinHttpConnect(hSession, URL.c_str(), portToUse, 0);
if (hConnect) {
hRequest = WinHttpOpenRequest(hConnect, protocol.c_str(), NULL, NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, 0);
if (!hRequest) {
std::cout << "WinHttpOpenRequest error " << GetLastErrorAsString();
}
}
else {
std::cout << "WinHttpConnect error " << GetLastErrorAsString();
}
}
else {
std::cout << "WinHttpOpen error " << GetLastErrorAsString();
}
为什么你对 WinHttpConnect
的调用失败
docs 告诉我们 WinHttpConnect
需要 服务器名称 ,而不是 URL:
pswzServerName [in]
Pointer to a null-terminated string that contains the host name of an HTTP server. Alternately, the string can contain the IP address of the site in ASCII, for example, 10.0.1.45.
但是您提供的字符串包含 URL,这是无效的。您需要将 "https://pages.awscloud.com/awsomedayonlineconference-reg.html"
更改为 "pages.awscloud.com"
,然后在 WinHttpOpenRequest
中提供页面路径作为参数:
hRequest = WinHttpOpenRequest(hConnect, protocol.c_str(), path.c_str(), NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, 0);
其中 path
是一个包含 "/awsomedayonlineconference-reg.html"
.
为此,您可以将 URL 拆分成多个部分,或者更改 httpConnect
方法以将服务器名和路径作为单独的参数。