#1 2019-02-24 10:43:02

jbroussia
Member
From: France
Registered: 2011-04-09
Posts: 74

Big newbie, how to retrieve STATUS_CODE when using TWinHttp ?

Hi,

Sorry, I'm going back to Delphi after a quite long break and I lost some obvious knowledge :-\ (and I crashed a few months ago, spent a couple of weeks in coma, it seems that some things are now damaged in my brain now... not complaining, just using this as an argument to get mercy for an answer :-p)

So, I'm trying to use TWinHttp to download the content of some web pages, the class is working as expected but I'd like 1- to have more control on the User-Agent  ("myClass(TWinHttp).UserAgent := 'blabla'" should be OK, right ?) and 2- to retrieve the status code from the server (200, 404, 2xx, 3xx, ...): how to do it with TWinHttp ?

Thanks !

Offline

#2 2019-02-25 15:40:52

damiand
Member
From: Greece
Registered: 2018-09-21
Posts: 94

Re: Big newbie, how to retrieve STATUS_CODE when using TWinHttp ?

At least for the status code, from another newbie tongue

The status code is contained in the first line of the OutHeaders string, e.g. (XXX is the placement of the status code, DESCR is the placement of the corresponding textual description, like OK, followed by CRLF):

HTTP/1.1 XXX DESCR#13#10

The OutHeaders string is an output parameter both in HttpGet and in TWinHTTP.Post:

var
  sOutHeaders, sResponse, sURL, sData, sHeaders: SockString;
...
sResponse := HttpGet(sURL, sHeaders, @sOutHeaders);
...
sResponse := TWinHTTP.Post(sURL, sData, sHeaders, true, @sOutHeaders);

Offline

#3 2019-02-25 16:16:46

mpv
Member
From: Ukraine
Registered: 2012-03-24
Posts: 1,571
Website

Re: Big newbie, how to retrieve STATUS_CODE when using TWinHttp ?

You do not need to parse headers manually. Just use a low level call.
This is cross platform solution:

var fClient: THttpRequest;

    fClient := {$IFDEF MSWINDOWS}TWinHTTP{$ELSE}TCurlHTTP{$ENDIF}
      .Create(aServer, aPort, aHttps, aProxyName, aProxyByPass, fConnectTimeout, fSendTimeout, fReceiveTimeout);

    try
      FResponseStatus := fClient.Request(URL, FMethod, keepAlive, fInHeaders, FInData, '', FRespHeaders, FRespText);
    except
      on E: EOSError do
        FResponseStatus := E.ErrorCode;
    end;

Offline

#4 2019-02-26 11:09:08

jbroussia
Member
From: France
Registered: 2011-04-09
Posts: 74

Re: Big newbie, how to retrieve STATUS_CODE when using TWinHttp ?

Thanks @damiand and @mpv for your help ! :-)

Offline

Board footer

Powered by FluxBB