Network: add lastResponseCode

This commit is contained in:
zvon 2020-01-17 14:01:10 +01:00
parent 8da68a5327
commit a9802b4d47
3 changed files with 29 additions and 4 deletions

View File

@ -3,8 +3,8 @@
#ifdef _WIN32 #ifdef _WIN32
#include <Windows.h>
#include <WinInet.h> #include <WinInet.h>
#include <Windows.h>
#include <string> #include <string>
using string = std::wstring; using string = std::wstring;
@ -28,12 +28,14 @@ public:
void clearHeader(); void clearHeader();
bool initSuccessful(); bool initSuccessful();
void setServer( const string &server ); void setServer( const string &server );
int lastResponseCode();
private: private:
#ifdef _WIN32 #ifdef _WIN32
HINTERNET _hInternet = nullptr; HINTERNET _hInternet = nullptr;
HINTERNET _hConnect = nullptr; HINTERNET _hConnect = nullptr;
std::wstring _headers = L""; std::wstring _headers = L"";
int status_code{};
#else #else
CURL *_curl_handle = nullptr; CURL *_curl_handle = nullptr;
struct curl_slist *_chunk = nullptr; struct curl_slist *_chunk = nullptr;

View File

@ -83,3 +83,9 @@ bool Request::initSuccessful() {
void Request::setServer( const string &server ) { void Request::setServer( const string &server ) {
_server = server; _server = server;
} }
int Request::lastResponseCode() {
long code{};
curl_easy_getinfo( _curl_handle, CURLINFO_RESPONSE_CODE, &code );
return code;
}

View File

@ -28,11 +28,15 @@ std::string sendRequest( HINTERNET hRequest, const std::wstring &headers,
std::unique_ptr< char > opt( new char[optional.length()] ); std::unique_ptr< char > opt( new char[optional.length()] );
memcpy( opt.get(), optional.c_str(), optional.length() ); memcpy( opt.get(), optional.c_str(), optional.length() );
auto requestSent = HttpSendRequest( auto requestSent =
hRequest, headers.c_str(), headers.length(), opt.get(), optional.length() ); HttpSendRequest( hRequest, headers.c_str(), headers.length(), opt.get(),
optional.length() );
if ( !requestSent ) if ( !requestSent ) {
status_code = -1;
std::wcerr << "ERROR HttpSendRequest: " << GetLastError() << std::endl; std::wcerr << "ERROR HttpSendRequest: " << GetLastError() << std::endl;
return "";
}
char dataReceived[4096]; char dataReceived[4096];
unsigned long numberOfBytesRead = 0; unsigned long numberOfBytesRead = 0;
@ -43,6 +47,13 @@ std::string sendRequest( HINTERNET hRequest, const std::wstring &headers,
response.append( dataReceived, numberOfBytesRead ); response.append( dataReceived, numberOfBytesRead );
} }
wchar_t responseText[256];
DWORD responseTextSize = sizeof( responseText );
HttpQueryInfo( hRequest, HTTP_QUERY_STATUS_CODE, &responseText,
&responseTextSize, NULL );
status_code = std::stoi( responseText );
return response; return response;
} }
@ -86,3 +97,9 @@ void Request::setServer( const string &server ) {
if ( !_hConnect ) if ( !_hConnect )
std::wcerr << "ERROR InternetConnect: " << GetLastError() << std::endl; std::wcerr << "ERROR InternetConnect: " << GetLastError() << std::endl;
} }
int Request::lastResponseCode() {
long code{};
curl_easy_getinfo( _curl_handle, CURLINFO_RESPONSE_CODE, &code );
return code;
}