#1 2016-03-16 13:24:01

fabioxgn
Member
Registered: 2015-11-06
Posts: 34

Allow configuring Proxy Username/Password on WinINet

I'm using WinINet because I have several users which uses proxy and it "just works" if the user have the proxy configured on Internet Explorer.

But when the proxy server requires authentication, it is necessary to configure it on the WinINet connection and I didn't find out how to do it. Is it implemented?

If not, is it possible to add this option? The microsoft documentation says you have to call InternetSetOption to do this: https://msdn.microsoft.com/en-us/librar … s.85).aspx

Here is the Proxy section:

Proxy Authentication
When a client attempts to use a proxy that requires authentication, the proxy returns a 407 status code message to the client. In that message, the proxy should include one or more Proxy-Authenticate response headers. These headers include the authentication methods available from the proxy. WinINet chooses the first method it recognizes.

The InternetErrorDlg function can be used to obtain the user name and password data from the user, or a customized user interface can be designed.

A custom interface can use the InternetSetOption function to set the INTERNET_OPTION_PROXY_PASSWORD and INTERNET_OPTION_PROXY_USERNAME values and then resend the request to the proxy.

I noticed that TWinInet already sets some params on connect:

procedure TWinINet.InternalConnect(ConnectionTimeOut,SendTimeout,ReceiveTimeout: DWORD);
var OpenType: integer;
begin
  if fProxyName='' then
   OpenType := INTERNET_OPEN_TYPE_PRECONFIG else
   OpenType := INTERNET_OPEN_TYPE_PROXY;
  fSession := InternetOpenA(Pointer(fUserAgent), OpenType,
    pointer(fProxyName), pointer(fProxyByPass), 0);
  if fSession=nil then
    raise EWinINet.Create;
  InternetSetOption(fConnection,INTERNET_OPTION_CONNECT_TIMEOUT,
    @ConnectionTimeOut,SizeOf(ConnectionTimeOut));
  InternetSetOption(fConnection,INTERNET_OPTION_SEND_TIMEOUT,
    @SendTimeout,SizeOf(SendTimeout));
  InternetSetOption(fConnection,INTERNET_OPTION_RECEIVE_TIMEOUT,
    @ReceiveTimeout,SizeOf(ReceiveTimeout));
  fConnection := InternetConnectA(fSession, pointer(fServer), fPort, nil, nil,
    INTERNET_SERVICE_HTTP, 0, 0);
  if fConnection=nil then
    raise EWinINet.Create;
end;

It would be nice if it could set the Proxy Auth too.

Offline

#2 2016-03-16 16:28:15

fabioxgn
Member
Registered: 2015-11-06
Posts: 34

Re: Allow configuring Proxy Username/Password on WinINet

After doing some tests, I was able to get my application to asks for the credentials. InternetSetOption didn't work for me, possible because I was doing something wrong.

I think that this approach would be better for the user as it asks for the credentialls and stores it on Windows.

I overwrote the WinInet classes, something like this:

procedure TProxyWinInet.InternalSetClass;
begin
  inherited;
  fRequestClass := TWinINetProxyAuth;
end;

function TWinINetProxyAuth.InternalRetrieveAnswer(var Header, Encoding, AcceptEncoding, Data: SockString): Integer;
begin
  Result := inherited;
  if Result = HTML_PROXYAUTHREQUIRED then
  begin
    FRetry := InternetErrorDlg(Application.Handle, fRequest, ERROR_INTERNET_INCORRECT_PASSWORD,
      FLAGS_ERROR_UI_FILTER_FOR_ERRORS or
      FLAGS_ERROR_UI_FLAGS_GENERATE_DATA or
      FLAGS_ERROR_UI_FLAGS_CHANGE_OPTIONS,
      Pointer(nil^)) = ERROR_INTERNET_FORCE_RETRY;
  end;
end;

function TWinINetProxyAuth.Request(const url, method: SockString; KeepAlive: Cardinal; const InHeader, InData,
  InDataType: SockString; out OutHeader, OutData: SockString): Integer;
begin
  repeat
    FRetry := False;
    Result := inherited;
  until not FRetry;
end;

This works and stores the credentials on Windows, but it would be nice if it was done automatically.

Offline

#3 2016-03-16 16:33:49

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,242
Website

Re: Allow configuring Proxy Username/Password on WinINet

WinHTTP could use the proxy configured in Internet Explorer.
See http://synopse.info/files/html/Synopse% … l#NDX_1521

Offline

#4 2016-03-16 16:57:41

fabioxgn
Member
Registered: 2015-11-06
Posts: 34

Re: Allow configuring Proxy Username/Password on WinINet

ab, yes but the credentials are not stored on the internet explorer config. If you have a proxy server with authentication you have to provide the username/password to WinINet.

Offline

Board footer

Powered by FluxBB