Home > Mobile >  WinHttpSendRequest GetLastError 6
WinHttpSendRequest GetLastError 6

Time:09-22

Just learning c + +, wanted to do some tests with the winhttp
 
Void QtGuiApplication: : getBtnClick () {
DWORD dwSize=0;
DWORD dwDownloaded=0;
LPSTR pszOutBuffer;
BOOL bResults=FALSE;
HINTERNET hSession=NULL,
HConnect=NULL,
HRequest=NULL;

//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)
//www.microsoft.com
HConnect=WinHttpConnect (hSession, L "127.0.0.1,"
INTERNET_DEFAULT_HTTPS_PORT, 0);


//Create an HTTP request handle.
If (hConnect) {
HRequest=WinHttpOpenRequest (hConnect, L "GET", NULL,
NULL, WINHTTP_NO_REFERER,
WINHTTP_DEFAULT_ACCEPT_TYPES,
WINHTTP_FLAG_SECURE);
}


//Send a request.
If (hRequest)
BResults=WinHttpSendRequest (hRequest,
WINHTTP_NO_ADDITIONAL_HEADERS,
WINHTTP_NO_REQUEST_DATA 0, 0,
0, 0);


//the End of the request.
If (bResults) {
BResults=WinHttpReceiveResponse (hRequest, NULL);
}
The else {
QDebug (" the Error from the WinHttpSendRequest ");
QString errorStr=QString: : number (GetLastError ());
QDebug (qPrintable (errorStr));
}


//Keep checking for data until there is nothing left.
If (bResults)
Do
{
//Check for the available data.
DwSize=0;
if (! WinHttpQueryDataAvailable (hRequest, & amp; DwSize))
Printf (" Error % u in WinHttpQueryDataAvailable. \ n ", GetLastError ());

//the Allocate space for the buffer.
PszOutBuffer=new char [dwSize + 1];
if (! PszOutBuffer)
{
Printf (" Out of memory \ n ");
DwSize=0;
}
The else
{
//Read the Data.
ZeroMemory (pszOutBuffer dwSize + 1);

if (! PszOutBuffer WinHttpReadData (hRequest, (LPVOID),
DwSize, & amp; DwDownloaded))
Printf (" Error % u in WinHttpReadData. \ n ", GetLastError ());
The else
QDebug (pszOutBuffer);

//Free the memory allocated to the buffer.
The delete [] pszOutBuffer;
}

} while (dwSize & gt; 0);


//Report any errors.
if (! BResults) {
QDebug (" Error has occurred ");
QString STR=QString: : number (GetLastError ());
QDebug (qPrintable (STR));
}


//Close any open handles.
If (hRequest) WinHttpCloseHandle (hRequest);
If (hConnect) WinHttpCloseHandle (hConnect);
If (hSession) WinHttpCloseHandle (hSession);
}




Here I have a server, I was literally use springboot 127.0.0.1 port 80, when browsing can be access directly, using the winhttp API, this step is not effective to WinHttpSendRequest handle, before is valid handle, such problems have bosses, qaq

  • Related