Witam jestem w trakcie przerabiania mojej klasy na bardziej użyteczną, chciałem dodać możliwość odbierania nagłówków od serwera. Problem pojawił się przy funkcji HttpQueryInfo. Która nie pokazuje mi co pobrała zamiast tego dostaje jakieś liczby w HEX które za każdym razem są inne. debuger http pokazuje mi że został zwrócony nagłówek. Sama funkcja HttpQueryInfo została pobrana stąd
#include <iostream>
#include <windows.h>
#include <wininet.h>
#include <TlHelp32.h>
#include <conio.h>
#include <tchar.h>
#include <sstream>
#include <string>
#include <Strsafe.h>
#pragma comment(lib,"Wininet.lib")
using namespace std;
class HttpSendForm {
private:
HINTERNET Initialize;
HINTERNET hinIneOpen;
unsigned int ErrorCode;
public:
string test;
DWORD aaaaa1;
HINTERNET Connection;
HINTERNET RequestOp;
inline BOOL InsternetOpen_( LPCWSTR lpstAgentName );
inline BOOL InternetConnect_( LPCTSTR lpstServerName );
inline BOOL HttpOpenRequest_( LPCTSTR lpstRequest, LPCTSTR lpstObjectName, LPCTSTR Version );
inline BOOL HttpAddRequestHeaders_( LPCWSTR lpstrHeader, DWORD size, DWORD Flag );
inline BOOL HttpSendRequest_( LPVOID lpOptional, DWORD dwOptionalLength );
inline BOOL HttpQueryInfo_();
inline void InternetExit();
};
inline BOOL HttpSendForm::HttpQueryInfo_() {
DWORD aa = 700;
HttpQueryInfo( this->RequestOp, HTTP_QUERY_RAW_HEADERS_CRLF, & test, & aa, & aaaaa1 );
cout << endl << GetLastError();
return 0;
}
inline BOOL HttpSendForm::InsternetOpen_( LPCWSTR lpstAgentName ) {
this->hinIneOpen = InternetOpen( lpstAgentName, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0 );
if( this->hinIneOpen == NULL ) return GetLastError();
else return 0;
}
inline BOOL HttpSendForm::InternetConnect_( LPCTSTR lpstServerName ) {
this->Connection = InternetConnect( this->hinIneOpen, lpstServerName, INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0 );
if( this->Connection == NULL ) return GetLastError();
else return 0;
}
inline BOOL HttpSendForm::HttpOpenRequest_( LPCTSTR lpstRequest, LPCTSTR lpstObjectName, LPCTSTR Version ) {
this->RequestOp = HttpOpenRequest( this->Connection, lpstRequest, lpstObjectName, Version, NULL, NULL, INTERNET_FLAG_NO_AUTO_REDIRECT, 0 );
if( this->RequestOp == NULL ) return GetLastError();
else return 0;
}
inline BOOL HttpSendForm::HttpAddRequestHeaders_( LPCWSTR lpstrHeader, DWORD size, DWORD Flag ) {
BOOL blResult = HttpAddRequestHeaders( this->RequestOp, lpstrHeader, size, Flag );
if( blResult == FALSE ) return GetLastError();
else return 0;
}
inline BOOL HttpSendForm::HttpSendRequest_( LPVOID lpOptional, DWORD dwOptionalLength ) {
BOOL blResulte = HttpSendRequest( this->RequestOp, NULL, NULL, lpOptional, dwOptionalLength );
if( this->RequestOp == NULL ) return GetLastError();
else return 0;
}
inline void HttpSendForm::InternetExit() {
InternetCloseHandle( this->hinIneOpen );
}
BOOL SampleCodeOne( HINTERNET hHttp )
{
LPVOID lpOutBuffer = NULL;
DWORD dwSize = 0;
retry:
if( !HttpQueryInfo( hHttp, HTTP_QUERY_CONTENT_TYPE,
( LPVOID ) lpOutBuffer, & dwSize, NULL ) )
{
cout << endl << "1. " << lpOutBuffer << endl;
if( GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND )
{
return TRUE;
}
else
{
if( GetLastError() == ERROR_INSUFFICIENT_BUFFER )
{
lpOutBuffer = new char[ dwSize + 100 ];
goto retry;
}
else
{
if( lpOutBuffer )
{
delete[] lpOutBuffer;
}
return FALSE;
}
}
}
cout << endl << "2. " << lpOutBuffer << endl;
if( lpOutBuffer )
{
cout << endl << "3. " << lpOutBuffer << endl;
delete[] lpOutBuffer;
}
return TRUE;
}
BOOL SampleCodeTwo( HINTERNET hHttp )
{
DWORD dwSize = 20;
LPVOID lpOutBuffer = new char[ dwSize ];
StringCchPrintfA(( LPSTR ) lpOutBuffer, dwSize, "Content-Type" );
retry:
if( !HttpQueryInfo( hHttp, HTTP_QUERY_CUSTOM,
( LPVOID ) lpOutBuffer, & dwSize, NULL ) )
{
if( GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND )
{
delete[] lpOutBuffer;
return TRUE;
}
else
{
if( GetLastError() == ERROR_INSUFFICIENT_BUFFER )
{
delete[] lpOutBuffer;
lpOutBuffer = new char[ dwSize ];
cout << lpOutBuffer;
StringCchPrintfA(( LPSTR ) lpOutBuffer,
dwSize, "Content-Type" );
goto retry;
}
else
{
delete[] lpOutBuffer;
return FALSE;
}
}
}
return TRUE;
}
int main()
{
char test[ 128 ] = "adasdsa";
HttpSendForm Test;
LPCWSTR header = L"Content-Type: application/x-www-form-urlencoded\rAccept-Language: en-US";
char lpoptional[ 32 ] = "nick=wdasde&pass=abc123";
cout << Test.InsternetOpen_( L"TestTwo" );
cout << Test.InternetConnect_( L"www.example-site.com" );
cout << Test.HttpOpenRequest_( L"POST", L"add.php", L"HTTP/1.1" );
cout << Test.HttpAddRequestHeaders_( header, 128, HTTP_ADDREQ_FLAG_ADD_IF_NEW );
if( SampleCodeOne( Test.RequestOp ) ) {
cout << "Good";
}
else cout << "SampleCodeOne Problem...";
cout << Test.HttpSendRequest_( test, 10 );
if( SampleCodeOne( Test.RequestOp ) ) {
cout << "Good";
}
else cout << "SampleCodeOne Problem...";
system( "pause" );
return 0;
}