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

[WinHTTP] WinHttpReceiveResponse + 302 Found = błąd

Ostatnio zmodyfikowano 2014-12-22 20:54
Autor Wiadomość
Admixior
Temat założony przez niniejszego użytkownika
[WinHTTP] WinHttpReceiveResponse + 302 Found = błąd
» 2014-12-15 20:46:21
Podczas używania biblioteki WinHTTP natknąłem się na problem taki, że jeżeli odpowiedź serwera zawiera kod 302 Found (z powodu przekierowania) to funkcja WinHttpReceiveResponse() powoduje błąd.

Jeżeli poprzez proxy zamienię 302 Found na 200 OK to wszystko działa poprawnie.

Najwygodniejszym dla mnie rozwiązaniem byłoby gdybym mógł pobrać zawartość danych przesłanych w pakiecie z 302 Found.

Jest ktoś w stanie naprowadzić lub podać powód?

Visual Studio 2010,
Windows 7 x64 (aczkolwiek aplikacja kompilowana pod x86),
Kod błędu zwracany przez GetLastError(): 13 ( The data is invalid. ) // w dokumentacji tej funkcji nie znalazłem wystąpienie tego błędu

Kawałek kodu:
C/C++
#include <stdio.h>
#include <windows.h>
#include <winhttp.h>
#include <Wincrypt.h>
#include <string>
using namespace std;
#pragma comment(lib, "crypt32.lib")

//#pragma comment(lib, "wininet.lib")
#pragma comment(lib, "winhttp.lib")

HINTERNET hSession = NULL,
hConnect = NULL,
hRequest = NULL;

string full_response;

int Zaloguj( string login, string pass );

int main( int argc, char * argv[] )
{
   
    hSession = WinHttpOpen( L"Ja",
    WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, 0, 0 );
   
    int a = Zaloguj( "abc", "effg" );
   
    if( hSession ) WinHttpCloseHandle( hSession );
   
    if( full_response.find( "Przekierowanie" ) !=- 1 ) printf( "LOGIN Success" );
    else if( full_response.find( "Nieprawid" ) !=- 1 ) printf( "LOGIN Fail" );
   
    system( "pause" );
    return a;
}

int Zaloguj( string login, string pass )
{
    if( hSession == NULL ) return 1337;
   
    if( hRequest ) WinHttpCloseHandle( hRequest );
   
    if( hConnect ) WinHttpCloseHandle( hConnect );
   
    hRequest = hConnect = NULL;
    DWORD PacketDataSize = 0;
    LPSTR pszOutBuffer;
    string data = "abecadlo";
   
    // Specify an HTTP server.
    if( hSession )
         if( !( hConnect = WinHttpConnect( hSession, L"jakasstrona.pl",
    INTERNET_DEFAULT_HTTPS_PORT, 0 ) ) ) return 1337;
   
    DWORD dwDownloaded = 0;
   
    if( !( hRequest = WinHttpOpenRequest( hConnect, L"POST", L"login", L"HTTP/1.1", 0, 0, WINHTTP_FLAG_SECURE ) ) )
         return GetLastError();
   
    DWORD temp = SECURITY_FLAG_IGNORE_UNKNOWN_CA
    | SECURITY_FLAG_IGNORE_CERT_DATE_INVALID
    | SECURITY_FLAG_IGNORE_CERT_CN_INVALID
    | SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE;
   
    if( !WinHttpSetOption( hRequest, WINHTTP_OPTION_SECURITY_FLAGS, & temp, sizeof( temp ) ) )
         return GetLastError();
   
    // Send a request.
   
    if( !( WinHttpSendRequest( hRequest, L"Content-Type: application/x-www-form-urlencoded", - 1,( void * ) data.c_str(), data.size(), data.size(), 0 ) ) )
         return GetLastError();
   
   
    // End the request.
    if( !WinHttpReceiveResponse( hRequest, NULL ) ) return GetLastError(); //w tym miejscu jest problem jeśli nagłówek zawiera 302 Found
   
   
    do
    {
        PacketDataSize = 0;
        if( !WinHttpQueryDataAvailable( hRequest, & PacketDataSize ) ) return GetLastError();
       
        pszOutBuffer = new( std::nothrow ) char[ PacketDataSize + 1 ];
        if( !pszOutBuffer )
        {
            printf( "Out of memory\n" );
            PacketDataSize = 0;
        }
        else
        {
            ZeroMemory( pszOutBuffer, PacketDataSize + 1 );
           
            if( !WinHttpReadData( hRequest,( LPVOID ) pszOutBuffer,
            PacketDataSize, & dwDownloaded ) )
            {
                delete[] pszOutBuffer;
                return GetLastError();
            }
            else full_response += pszOutBuffer;
           
            delete[] pszOutBuffer;
        }
    } while( PacketDataSize > 0 );
   
   
    if( hRequest ) WinHttpCloseHandle( hRequest );
   
    if( hConnect ) WinHttpCloseHandle( hConnect );
   
    hRequest = hConnect = NULL;
    return 0;
}
P-122906
DejaVu
» 2014-12-21 22:48:05
302 Found means: The current URL does not exists, but the document was found at another location. The browser should look there. Since you have disabled redirects, you have to do this manually. Read the loaction header, which should contain the new url.
P-123210
Admixior
Temat założony przez niniejszego użytkownika
» 2014-12-22 20:54:29
Finalnie pomogło użycie funkcji: WinHttpSetOption. I ustawienie (tak jak pisało w jednym linku) flagi wyłączającej automatyczne przekierowanie.

Być może było to spowodowane przejściem https->http, gdyż zapytanie miało ustawione port 443 z połączeniem SSL.

Dzięki za odp.
P-123242
« 1 »
  Strona 1 z 1