9

I am trying to use TIdHTTP.Get to load the USPS Zip4 result web page source into a variable (to extract the 4 digit zip code suffix), e.g.,

PgSrc := IdHTTP1.Get('https://tools.usps.com/go/ZipLookupResultsAction!input.action?resultMode=0&companyName=&address1=1600+PENNSYLVANIA+AVE+NW&address2=&city=&state=Select&urbanCode=&postalCode=&zip=20500');

The above example url works fine if I paste it into any browser. However, in my code I get an EIdIOHandlerPropInvalid error with the following message "IOHandler value is not valid."

I have many zip codes to look up, so I would appreciate any help to avoid this error or a suggestion for a different approach.

Max Williams
  • 821
  • 1
  • 7
  • 13
  • Does the Get work with other URLs? Maybe the component is not initialized properly. Have you configured it for SSL? See http://stackoverflow.com/questions/6683127/delphi-idhttpssl – mjn Jul 19 '12 at 11:57
  • @mjn I had not configured it for SSL – Max Williams Jul 19 '12 at 17:36

1 Answers1

27

To avoid this exception you must assign the IOHandler property.

Check this sample.

{$APPTYPE CONSOLE}

{$R *.res}

uses
  IdHTTP,
  IdSSLOpenSSL,
  SysUtils;

Var
  IdHTTP1 : TIdHTTP;
  Src : string;
  LHandler: TIdSSLIOHandlerSocketOpenSSL;
begin
  try
   IdHTTP1:=TIdHTTP.Create(nil);
   try
    LHandler := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
    try
      IdHTTP1.IOHandler:=LHandler;
      Src:= IdHTTP1.Get('https://tools.usps.com/go/ZipLookupResultsAction!input.action?resultMode=0&companyName=&address1=1600+PENNSYLVANIA+AVE+NW&address2=&city=&state=Select&urbanCode=&postalCode=&zip=20500');
      Writeln(Src);
    finally
      LHandler.Free;
    end;
   finally
     IdHTTP1.Free;
   end;
  except on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
  Readln;
end.

Note : Also remember copy the SSL DLL (libeay32.dll, ssleay32.dll) to your system.

RRUZ
  • 134,889
  • 20
  • 356
  • 483
  • Note that if you are using an up-to-date version of Indy, `TIdHTTP` can now create a default SSLIOHandler for you if the first request is for an HTTPS URL. See [New HTTPS functionality for TIdHTTP](http://www.indyproject.org/sockets/blogs/changelog/20141222.aspx) for details. – Remy Lebeau Apr 03 '17 at 16:15
  • Just for clarification, I would add that the problem is related to not configuring SSL properly, otherwise, the answer is excellent... – Radek Hladík Jun 29 '17 at 10:08