-2
#include <windows.h>
#include <winhttp.h>
#include <stdio.h>
#pragma comment(lib, "winhttp.lib")
int main()
{   
    static BOOL bRet = FALSE;
    TCHAR szHostName[MAX_PATH] = L"www.google.com";
    HINTERNET hSession = NULL;
    HINTERNET hConnect = NULL;
    HINTERNET hRequest = NULL;
    const TCHAR BROWSER_INFO[] = L"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 1.1.4322; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
    const WCHAR *WWIZHTTP_TYPES[] = { L"Accept: image/gif", L"image/x-xbitmap", L"image/jpeg", L"image/pjpeg", L"application/x-shockwave-flash", L"application/x-ms-application", L"application/x-ms-xbap", L"application/vnd.ms-xpsdocument", L"application/xaml+xml", L"application/msword", L"application/vnd.ms-excel", L"application/x-cabinet-win32-x86", L"application/x-pe-win32-x86", L"application/octet-stream", L"application/x-setupscript", L"*//*", NULL };

    hSession = WinHttpOpen(BROWSER_INFO, WINHTTP_ACCESS_TYPE_NAMED_PROXY, L"172.168.1.196:808", L"<local>", 0);

    if (hSession)
    {
        hConnect = WinHttpConnect(hSession, szHostName, INTERNET_DEFAULT_HTTP_PORT, 0);
    }
    if (hConnect)
    {
        hRequest = WinHttpOpenRequest(hConnect, L"GET", NULL, NULL, WINHTTP_NO_REFERER, WWIZHTTP_TYPES, 0);
    }
    if (hRequest)
    {

        //provide username and password

        WinHttpSetCredentials(hRequest, WINHTTP_AUTH_TARGET_PROXY, WINHTTP_AUTH_SCHEME_BASIC, L"abc", L"abc", NULL);

        // Send a request.
        bRet = WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0);

        // End the request.         
        if (bRet)
            bRet = WinHttpReceiveResponse(hRequest, NULL);

        if (bRet)
        {
            DWORD dwStatusCode = 0;
            DWORD dwTemp = sizeof(dwStatusCode);
            WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER, NULL, &dwStatusCode, &dwTemp, NULL);
            if ((dwStatusCode >= HTTP_STATUS_CONTINUE) && (dwStatusCode < HTTP_STATUS_BAD_REQUEST))
            {
                bRet = TRUE;
            }
            else
            {
                bRet = FALSE;
            }
        }
        if (bRet == TRUE)
            MessageBox(NULL, L"HIT", L"",0);
    }
    return 0;
}

In above code, return value of dwStatusCode from WinHttpQueryHeaders() is 407 when username-password is set for proxy server. Here WinHttpSetCredentials() is used to provide username and password but still return value of dwStatusCode is 407. If proxy server is not having username-password then above code works as per requirement i.e., return value of dwStatusCode is 200. But in case of authentication this code fails.

I am using CCProxy version 8.0 for this sample. Please guide with proper solution.

  • You don't check return value of `WinHttpSetCredentials()`. If it returns `FALSE`, call `GetLastError()` to get extended error information. – zett42 Nov 16 '18 at 14:07
  • With that have you run it successfully? Because in above code I didn't receive any error. I have checked using 'GetLastError()' api. If you have not run code please don't post any comment. I am looking for running solution. – kunal waghmare Nov 19 '18 at 05:45

1 Answers1

0

Please pay attention to the example in this document, If a proxy authentication challenge was responded to, reset those credentials before each SendRequest, because the proxy may require re-authentication after responding.

And if still not working, you may need to use WinHttpSetOption instead of WinHttpSetCredentials to set proxy authentication, example:

if (!WinHttpSetOption(session, WINHTTP_OPTION_PROXY_USERNAME, proxy_username, proxy_username_len)) 
{
    //do error operation.
}
if (!WinHttpSetOption(session, WINHTTP_OPTION_PROXY_PASSWORD, proxy_password, proxy_password_len)) 
{
    //do error operation.
}
Drake Wu
  • 6,218
  • 1
  • 4
  • 21
  • @kunal waghmare, you may need to use `WinHttpSetOption` instead of `WinHttpSetCredentials` to set proxy authentication according to MSDN document. I have added an example in my answer. – Drake Wu Nov 23 '18 at 03:00