Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi THTTPRio how to use authenticating proxy server

I've used the WSDL importer with Delphi XE2 and it has generate a routine that looks like the following, excluding the 3 commented lines where I'm attempting to use a proxy server.

function GetIXYZService(UseWSDL: Boolean; Addr: string; HTTPRIO: THTTPRIO): IXYZService;
const
  defWSDL = 'https://server/XYZService.svc?wsdl';
  defURL  = 'https://server/XYZService.svc';
  defSvc  = 'Company.XYZ.Services.XYZService';
  defPrt  = 'BasicHttpBinding_IXYZService';
var
  RIO: THTTPRIO;
begin
  Result := nil;
  if (Addr = '') then
  begin
    if UseWSDL then
      Addr := defWSDL
    else
      Addr := defURL;
  end;
  if HTTPRIO = nil then
    RIO := THTTPRIO.Create(nil)
  else
    RIO := HTTPRIO;
  try
    // RIO.HTTPWebNode.Proxy := 'server_ip:port';
    // RIO.HTTPWebNode.Username := 'username';
    // RIO.HTTPWebNode.Password := 'password';

    Result := (RIO as ISSOService);
    if UseWSDL then
    begin
      RIO.WSDLLocation := Addr;
      RIO.Service := defSvc;
      RIO.Port := defPrt;
    end else
      RIO.URL := Addr;
  finally
    if (Result = nil) and (HTTPRIO = nil) then
      RIO.Free;
  end;
end;

I need to access the service through an authenticating proxy server. I've added the 3 lines shown above and when I uncomment them I cannot connect. The help for THTTPRio states...

If you need to use a proxy server, or if the server requires authentication, use the properties of the THTTPReqResp object that is the value of the HTTPWebNode property to provide the necessary information.

This I have done, but when I attempt to use my service an ESOAPHTTPException is raised having message...

Unauthorized (407) - 'https://server/XYZService.svc'

I've stumbled upon this post that says to set the proxy settings after setting the WSDLLocation, Service, and Port which I've tried with no success.

http://www.delphigroups.info/2/10/555621.html

I am also not building with USE_INDY defined. My service uses SSL so I'm using WinInet.

I am not sure what is wrong with this approach so any help is appreciated.

Thanks, Michael

like image 222
Michael S. Avatar asked Jul 31 '12 14:07

Michael S.


1 Answers

According to this, there are 2 ways to set proxy authorization for wininet. I haven't seen any HTTP_STATUS_PROXY_AUTH_REQ in the source code of Soap.SOAPHTTPTrans, so you probably have to write your own handling of this error. To do that, set THTTPReqResp.OnWinInetError handler and process HTTP_STATUS_PROXY_AUTH_REQ error code.

Or you may try to catch the OnBeforePost of THTTPReqResp and call HttpAddRequestHeaders with base64 encoded pair of login and password. Hope it helps.

like image 161
Sega-Zero Avatar answered Nov 10 '22 03:11

Sega-Zero