Martin Schweizer
Martin Schweizer

Reputation: 11

Can't send Proxy credentials (407)

I am trying do code C++ program that connects with a proxy which needs password and username authentication (ip:port:username:pw) I got http working but when I am using https I always getting a 407 error.

How do I send the proxy credentials on https the right way? (C++)

Upvotes: 1

Views: 940

Answers (1)

Cosmin Balan
Cosmin Balan

Reputation: 47

Well that's good because the status 407 means the proxy requires authentication.

So you can use this:

    case 407:
            // The proxy requires authentication.
            printf( "The proxy requires authentication.  Sending credentials...\n" );

            // Obtain the supported and preferred schemes.
            bResults = WinHttpQueryAuthSchemes( hRequest, 
                &dwSupportedSchemes, 
                &dwFirstScheme, 
                &dwTarget );

            // Set the credentials before resending the request.
            if( bResults )
                dwProxyAuthScheme = ChooseAuthScheme(dwSupportedSchemes);

            // If the same credentials are requested twice, abort the
            // request.  For simplicity, this sample does not check 
            // for a repeated sequence of status codes.
            if( dwLastStatus == 407 )
                bDone = TRUE;
            break;

The function

    DWORD ChooseAuthScheme( DWORD dwSupportedSchemes )
  {
//  It is the server's responsibility only to accept 
//  authentication schemes that provide a sufficient
//  level of security to protect the servers resources.
//
//  The client is also obligated only to use an authentication
//  scheme that adequately protects its username and password.
//
//  Thus, this sample code does not use Basic authentication  
//  becaus Basic authentication exposes the client's username
//  and password to anyone monitoring the connection.

if( dwSupportedSchemes & WINHTTP_AUTH_SCHEME_NEGOTIATE )
    return WINHTTP_AUTH_SCHEME_NEGOTIATE;
else if( dwSupportedSchemes & WINHTTP_AUTH_SCHEME_NTLM )
    return WINHTTP_AUTH_SCHEME_NTLM;
else if( dwSupportedSchemes & WINHTTP_AUTH_SCHEME_PASSPORT )
    return WINHTTP_AUTH_SCHEME_PASSPORT;
else if( dwSupportedSchemes & WINHTTP_AUTH_SCHEME_DIGEST )
    return WINHTTP_AUTH_SCHEME_DIGEST;
else
    return 0;
   } 

This determines the authentication scheme.....and after that you use

    bResults = WinHttpSetCredentials( hRequest, 
        WINHTTP_AUTH_TARGET_SERVER, 
        dwProxyAuthScheme,
        username,
        password,
        NULL );

I hope this helps...i'm also working with these to connect to microsoft translator from azure marketplace, since it moved there and from august all the old bing translators are not gonna get requests. And for me it is to send the authentication key through the header. But i guess you have a username and password.

Upvotes: 3

Related Questions