Pregunta

I am trying to set user name and password for proxy using InternetSetOption(...) method. However, it always returns zero and Last error is set to 12018. Below is my code snippet.

#include "stdafx.h"
#include <Wininet.h>


int _tmain(int argc, _TCHAR* argv[])
{
    HINTERNET _session = ::InternetOpen(_T("TestProgram"),
                              INTERNET_OPEN_TYPE_PRECONFIG,
                              NULL, NULL, NULL);
    LPCTSTR proxyUserName = L"username";
    LPCTSTR proxyPassword = L"userpassword";
    BOOL b = ::InternetSetOption(_session,INTERNET_OPTION_PROXY_USERNAME ,(LPVOID)proxyUserName,wcslen(proxyUserName)+1 );
        printf(" InternetSetOption returns - %d\n",b);
        printf(" InternetSetOption GetLastError - %d\n",GetLastError());

        b = ::InternetSetOption(_session,INTERNET_OPTION_PROXY_PASSWORD,(LPVOID)proxyPassword,wcslen(proxyPassword) +1 );
        printf(" InternetSetOption returns - %d\n",b);
        printf(" InternetSetOption GetLastError - %d\n",GetLastError());

        getchar();
    return 0;
}

I tried to execute above program with Admin and non-admin permissions but no luck. Any help is welcome.

Thanks, Omky

¿Fue útil?

Solución

You have used incorrect HINTERNET handle. From Option Flags documentation:

INTERNET_OPTION_PROXY_USERNAME This option can be set on the handle returned by InternetConnect or HttpOpenRequest.

INTERNET_OPTION_PROXY_PASSWORD This option can be set on the handle returned by InternetConnect or HttpOpenRequest.

So, you must use InternetConnect() or HttpOpenRequest().

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top