Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

[WinInet] HttpQueryInfo problem z Headers

Ostatnio zmodyfikowano 2013-10-17 11:49
Autor Wiadomość
Am0x
Temat założony przez niniejszego użytkownika
[WinInet] HttpQueryInfo problem z Headers
» 2013-10-04 17:29:01
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   http://msdn.microsoft.com​/en-us/library/windows/desktop​/aa385373(v=vs.85).aspx

C/C++
#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:
   
    // This call will fail on the first pass, because
    // no buffer is allocated.
    if( !HttpQueryInfo( hHttp, HTTP_QUERY_CONTENT_TYPE,
    ( LPVOID ) lpOutBuffer, & dwSize, NULL ) )
    {
        cout << endl << "1. " << lpOutBuffer << endl;
        if( GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND )
        {
            // Code to handle the case where the header isn't available.
            return TRUE;
        }
        else
        {
            // Check for an insufficient buffer.
            if( GetLastError() == ERROR_INSUFFICIENT_BUFFER )
            {
                // Allocate the necessary buffer.
                lpOutBuffer = new char[ dwSize + 100 ];
               
                // Retry the call.
                goto retry;
            }
            else
            {
                // Error handling code.
                if( lpOutBuffer )
                {
                    delete[] lpOutBuffer;
                }
                return FALSE;
            }
        }
    }
    cout << endl << "2. " << lpOutBuffer << endl;
    if( lpOutBuffer )
    {
        cout << endl << "3. " << lpOutBuffer << endl;
        delete[] lpOutBuffer;
    }
   
    return TRUE;
}







// Retrieving Headers Using HTTP_QUERY_CUSTOM
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 )
        {
            // Code to handle the case where the header isn't available.
            delete[] lpOutBuffer;
            return TRUE;
        }
        else
        {
            // Check for an insufficient buffer.
            if( GetLastError() == ERROR_INSUFFICIENT_BUFFER )
            {
                // Allocate the necessary buffer.
                delete[] lpOutBuffer;
                lpOutBuffer = new char[ dwSize ];
                cout << lpOutBuffer;
                // Rewrite the header name in the buffer.
                StringCchPrintfA(( LPSTR ) lpOutBuffer,
                dwSize, "Content-Type" );
               
                // Retry the call.
                goto retry;
            }
            else
            {
                // Error handling code.
                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...";
    //Test.HttpQueryInfo_();
    //cout<<Test.test;
   
    system( "pause" );
    return 0;
}
P-93091
DejaVu
» 2013-10-17 11:49:22
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.
Skoro dostajesz hex-a to zamień hex-a na znaki, tj. dwa hexy = jeden bajt, czyli jeden znak.
P-93864
« 1 »
  Strona 1 z 1