[Lazarus] lnet and http headers

ADTEC (Pty) Ltd adtec at adtec.co.za
Wed Jul 6 12:59:55 CEST 2011


> Hi
>

Hi,


>
> I'm currently using synapse to connect to a website that will send a
> redirect instruction in one of the http headers. Unfortunately, I
> can't get it to work in 64bit. I'm trying to convert to the lnet
> package but can't see how to access the headers. Does anyone know if
> the package is capable of returning individual headers? If so, do you
> have an example I can view?
>

https://github.com/ik5/xmlrpc-client-ui/blob/master/src/untxmlrpc_client.pp#L347

But why not to publish the problematic code so we can help you better ?


>
> Thanks
>
> Graham
>

Ido

Thanks, Ido.

However, the way I see your code, it adds headers to send to the
server. I need to read headers coming back from the server.

Below is my code using synapse in a Win32 environment. The url
variable is set earlier. This code connects to the server and, because
I'm in the process of changing servers, responds with a header
"X-Redirect" that I use to query the next server in line. The code
works perfectly in 32 bit, but when I try to compile synapse in win64,
it fails because the synapse code assumes that if it is not WIN32, it
is being compiled for linux and wants the BaseUnix and Unix files.

  while bDone = False do
  begin
    frmConnecting := TfrmConnecting.Create(Self);
    frmConnecting.Show;
    frmConnecting.Update;

    HTTP := THTTPSend.Create;
    try
      frmConnecting.Label1.Caption := 'Connecting to server...';
      frmConnecting.Update;

      bResult := HTTP.HTTPMethod('GET', url);
      if bResult then
      begin
        frmConnecting.Label1.Caption := 'Checking result...';
        frmConnecting.Update;
        for i:=0 to HTTP.Headers.Count -1 do
        begin
             if LeftStr(HTTP.Headers[i], 9) = 'X-Result:' then
                sResult := RightStr(HTTP.Headers[i],
length(HTTP.Headers[i]) - 10);
             if LeftStr(HTTP.Headers[i], 11) = 'X-Redirect:' then
                url := RightStr(HTTP.Headers[i], length(HTTP.Headers[i]) - 12);
             if leftstr(HTTP.Headers[i],32) = 'Content-disposition:
attachment;' then
             begin
                  sFilename := RightStr(HTTP.Headers[i],
length(HTTP.Headers[i]) - pos('filename=', HTTP.Headers[i]) - 9);
                  sFilename := LeftStr(sFilename, length(sFilename) - 1);
             end;
        end;
        if sFilename <> '' then
        begin
              frmConnecting.Label1.Caption := 'Downloading file...';
              frmConnecting.Update;
              try
                 fStream := TFileStream.Create(gPriceFileDir + '\' +
sFilename, fmOpenWrite or fmCreate);
                 fStream.Seek(0, soFromBeginning);
                 fStream.CopyFrom(HTTP.Document, 0);
              finally
                 fStream.Free;
              end;
        end;
      end;
    finally
      HTTP.Free;
    end;
    frmConnecting.Close;
    // Set result message
    bDone := True;
    msg := '';
    Case StrToInt(LeftStr(sResult, 1)) of
         0 : msg := sFilename + ' successfully downloaded.';
         1 : msg := 'We have no record of you purchasing the software.';
         2 : msg := 'Your subscription has expired. No data downloaded.';
         3 : msg := 'Invalid username or password. No data downloaded.';
         4 : bDone := False;
    else
         msg := 'Unrecognized response code. Please check for software
upgrade.';
    end;
    if Length(msg) > 0 then
       ShowMessage(msg);
  end;

Thanks
Graham




More information about the Lazarus mailing list