#1 2019-03-03 09:35:36

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

TWinHttp vs HttpGet, why this side effect (and how to solve it) ?

Hi,

I'm trying to use an inherited TWinHttp (from THttpRequest) to download the content from an URL.
It *seems* to work... except that the result is not what's expected ! The content is retrieved (status code = 200), but is different from the (expected) one I get when using the HttpGet (from SynCtrlSock too) !?

Sample ex.:

- With WinHttp...

function blabla.GetContent1: SockString;
var
	Http: {$IFDEF MSWINDOWS}TWinHTTP{$ELSE}TCurlHTTP{$ENDIF};
	sInHeaders, sInData, sOutHeaders: SockString;
	sUrl: string;
	iResponseStatus: Integer;
begin
	Result := '';

	sUrl := 'https://www.betexplorer.com/soccer/';
	Http := {$IFDEF MSWINDOWS}TWinHTTP{$ELSE}TCurlHTTP{$ENDIF}.Create(Url);
	try
		iResponseStatus := Http.Request(sUrl, 'GET', 0, sInHeaders, sInData, '', sOutHeaders, Result);
	finally
		Http.Free;
	end;
end;

- With HttpGet

function blabla.GetContent2: SockString;
var
	sUrl: string;
begin
	sUrl := 'https://www.betexplorer.com/soccer/';
	Result := HttpGet(sUrl);
end;

So, the URL is specific, it may work in a different way on other URLs but that's the one I need to get. On the first case the content seems to be from the "main" page, while the second comes from the requested page ("soccer" page).

Did I miss some parameter when using TWinHttp ? Is something happening on the external server that detects the origin from the request to send it a different content ? I can't understand what's happening between the two cases :-\

Thanks.

Offline

#2 2019-03-03 12:39:07

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

Re: TWinHttp vs HttpGet, why this side effect (and how to solve it) ?

It's a little confusing in first parameter name of  Request method - it expect a path, not a full URL. So first example should be:

sUrl := 'https://www.betexplorer.com'; sPath = '/soccer/'
Http := {$IFDEF MSWINDOWS}TWinHTTP{$ELSE}TCurlHTTP{$ENDIF}.Create(sUrl);
try
  iResponseStatus := Http.Request(sPath, 'GET', 0, sInHeaders, sInData, '', sOutHeaders, Result);
finally
  Http.Free;
end;

Inside HttpGet source you can see how to parce URL onto parts using URI.from(URL)

Offline

#3 2019-03-03 13:09:56

profh
Member
Registered: 2010-07-02
Posts: 161

Re: TWinHttp vs HttpGet, why this side effect (and how to solve it) ?

@mpv, how to disable urlencode of sPath?

thanks!

Offline

#4 2019-03-03 15:43:54

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

Re: TWinHttp vs HttpGet, why this side effect (and how to solve it) ?

Ooooh ! Thanks again @mpv !
(BTW I already said that somewhere on this forum, but we really need a "thumbs up" button ! No obvious way to thankfully mention the great help and the incredible work of ab :-p)

Offline

Board footer

Powered by FluxBB