#1 2017-03-27 04:10:36

Del
Member
Registered: 2017-03-24
Posts: 20

TWinHTTP exception handling

I am using TWinHTTP, specifically Get and Put methods which seem to suppress any exceptions (404 etc) just returning an empty payload. I would like to handle / be aware of error conditions such as  these in my application however. What would be the correct way of facilitating this?

Thanks

Offline

#2 2017-03-27 04:51:47

igors233
Member
Registered: 2012-09-10
Posts: 241

Re: TWinHTTP exception handling

I think you'll have to call Request method instead. Get/Put calls are just handy wrappers around it but without HTTP return value.

Offline

#3 2017-03-27 08:16:50

Del
Member
Registered: 2017-03-24
Posts: 20

Re: TWinHTTP exception handling

I would appreciate an example of Request method with GET and POST method, i've tried

res : Integer;
outHDr, OutData : SockString;

res := fclient.Request('http://localhost:3002/testroute', 'GET', 0, Hdr.Header, EmptyStr, EmptyStr, outHDr, OutData);

where Hdr.Header provides Header string collection (works for GET method)
same url used for GET method and works but for Request method returns a 404.

I could not see any example or documentation outlining the Request method in detail.

Thanks

Offline

#4 2017-03-27 16:12:37

igors233
Member
Registered: 2012-09-10
Posts: 241

Re: TWinHTTP exception handling

> I would appreciate an example of Request method with GET and POST method, i've tried

var
  http: TWinHTTP;
  res: Integer;
  resHdr, resData, resDataGet: SockString;
begin
  resDataGet := TWinHTTP.Get('www.google.com');
  mmo1.Lines.Add(resDataGet);

  http := TWinHttp.Create('www.google.com', '80', False);
  res := http.Request('', 'GET', 0, '', '', '', resHdr, resData);
  if res = 200 then
    mmo1.Lines.Add(resData);

Offline

#5 2017-03-28 00:43:46

Del
Member
Registered: 2017-03-24
Posts: 20

Re: TWinHTTP exception handling

Thanks igors233, I should have just passed the relative route to the request instead of full URI. Example much appreciated.

Offline

#6 2017-03-28 06:56:20

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

Re: TWinHTTP exception handling

You have the TURI object to compute this "relative" URI decoding into server name and URI.

Offline

#7 2017-03-28 07:23:29

Del
Member
Registered: 2017-03-24
Posts: 20

Re: TWinHTTP exception handling

Thanks Arnaud, did not know about TURI. Will have a look. There are loads of functionality around the periphery of mormot that is very useful but with very little mention / examples. I find myself having to ask questions here due to this. I must give thanks for an awesome framework though, has already saved me loads of time:)

Offline

Board footer

Powered by FluxBB