I am trying to do some post.
Uses
IdBaseComponent, IdTCPConnection, IdTCPClient, IdHTTP, IdComponent, StrUtils,
IdSSLOpenSSL;
function PostData(const AURL: string; AParamList: TStringList): string;
var
_idHTTP: TIdHTTP;
_ResultStream: TStringStream;
_IdSSLIOHandler: TIdSSLIOHandlerSocketOpenSSL;
begin
_ResultStream := TStringStream.Create('', TEncoding.UTF8);
_idHTTP := TIdHTTP.Create(nil);
_IdSSLIOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
try
_idHTTP.IOHandler := _IdSSLIOHandler;
_idHTTP.Post(AURL, AParamList, _ResultStream);
Result := _ResultStream.DataString;
finally
FreeAndNil(_ResultStream);
FreeAndNil(_idHTTP);
FreeAndNil(_IdSSLIOHandler);
end;
end;
procedure TForm1.BitBtn1Click(Sender: TObject);
var
_ParamList: TStringList;
_Reply: string;
begin
_ParamList := TStringList.Create;
try
_ParamList.Add('id=276683492511635');
_ParamList.Add('pin=4580');
_Reply := PostData('https://eturundus.eu/samsung-pin-lt/_ajax/pin',
_ParamList);
ShowMessage(_Reply);
finally
FreeAndNil(_ParamList);
end;
end;
I should get { "status":"lose","tries":2 }
or { "status":"over" }
, etc, but all I am getting is an empty string in a message box. What I am missing?
Edit: I have added
_idHTTP.Request.UserAgent := 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:29.0) Gecko/20100101 Firefox/29.0';
_idHTTP.Request.ContentType := 'application/json, text/javascript, */*; q=0.01';
But still getting empty string.
There is what Tamper Data also provides me:
<?xml version="1.0"?>
<!-- Export of tamperdata transacions -->
<tdRequests>
<tdRequest uri="https%3A//eturundus.eu/samsung-pin-lt/_ajax/pin%3Fid%3D638369916257545%26pin%3D4580">
<tdStartTime>20:02:42.238</tdStartTime>
<tdStartTimeMS>1399827762238</tdStartTimeMS>
<tdElapsedTime>177</tdElapsedTime>
<tdTotalElapsedTime>177</tdTotalElapsedTime>
<tdStatus>200</tdStatus>
<tdStatusText>OK</tdStatusText><tdRequestMethod>GET</tdRequestMethod>
<tdContentSize>-1</tdContentSize>
<tdMimeType>text/html</tdMimeType>
<tdRequestHeaders>
<tdRequestHeader name="Host">eturundus.eu</tdRequestHeader>
<tdRequestHeader name="User-Agent">Mozilla/5.0%20%28Windows%20NT%206.1%3B%20WOW64%3B%20rv%3A29.0%29%20Gecko/20100101%20Firefox/29.0</tdRequestHeader>
<tdRequestHeader name="Accept">application/json%2C%20text/javascript%2C%20*/*%3B%20q%3D0.01</tdRequestHeader>
<tdRequestHeader name="Accept-Language">en-US%2Cen%3Bq%3D0.5</tdRequestHeader>
<tdRequestHeader name="Accept-Encoding">gzip%2C%20deflate</tdRequestHeader>
<tdRequestHeader name="X-Requested-With">
XMLHttpRequest</tdRequestHeader>
<tdRequestHeader name="Referer">
https%3A//eturundus.eu/samsung-pin-lt/</tdRequestHeader>
<tdRequestHeader name="Cookie">
fbsr_265895733592477%3DMVuDCQySiQZErCLlQhy2Z-NISg3-pNSug5Hkw7Kxr58.eyJhbGdvcml0aG0iOiJITUFDLVNIQTI1NiIsImNvZGUiOiJBUUNTWHUwdTFOQVRkTjRmSDZjUnktV0d3OFdSdUwyc3N1a0tkNEhlSjdVSUVaWlp0cWhNaWQ2TmtXX3J6ZFVRckdTXzZEZElnNW5oNW9GWkViWEl2cVVad2RNbVdLRGdVbGNpV1FsZnBwYlM2bnBLWENVZHNmcG1LNXM1ZlJ4REFSdWs2SXZMcUlVVEd0cUlVM0FoN2tid01DYnpoTWZsdnlPUUJmX0lTeGpENWRIaUpMbDM2VE0tNU85cHNRbGRxak1fSWlhdHMxTDRkZndDbWYwTkxFaFRoSTdnUk5mV3pRMlBwbWUzVWEtQXBVVHZRTXRQX0luTlpNR283M2duRWlpVDkzaVU3a2NDQTdTSFM5OXZ6WEVSLVBKQVkzek1PNUZBZ1NQZ09pZmZpckdmbTd5Z1BaVVNzblBrQ2pla19hQlYyRzRGQlZpa2ctaVp0QWJyazFwaCIsImlzc3VlZF9hdCI6MTM5OTgyNzcwNCwidXNlcl9pZCI6IjYzODM2OTkxNjI1NzU0NSJ9%3B%20PHPSESSID%3D58416520248001a68a259f52b7cf82dd</tdRequestHeader>
<tdRequestHeader name="Connection">
keep-alive</tdRequestHeader>
</tdRequestHeaders><tdPostHeaders></tdPostHeaders>
<tdPostElements>
</tdPostElements>
<tdResponseHeaders><tdResponseHeader name="Date">
Sun%2C%2011%20May%202014%2017%3A02%3A17%20GMT
</tdResponseHeader>
<tdResponseHeader name="Server">
Apache%20/%20DataZone
</tdResponseHeader>
<tdResponseHeader name="Expires">
Thu%2C%2019%20Nov%201981%2008%3A52%3A00%20GMT
</tdResponseHeader>
<tdResponseHeader name="Cache-Control">
no-store%2C%20no-cache%2C%20must-revalidate%2C%20post-check%3D0%2C%20pre-check%3D0
</tdResponseHeader>
<tdResponseHeader name="Pragma">
no-cache
</tdResponseHeader>
<tdResponseHeader name="Keep-Alive">
timeout%3D5%2C%20max%3D100
</tdResponseHeader>
<tdResponseHeader name="Connection">
Keep-Alive
</tdResponseHeader>
<tdResponseHeader name="Transfer-Encoding">
chunked
</tdResponseHeader>
<tdResponseHeader name="Content-Type">
text/html
</tdResponseHeader>
</tdResponseHeaders>
</tdRequest>
</tdRequests>
Don't use a TStringStream
at all. You are forcing UTF-8 decoding with it, but if the reply data is not UTF-8 encoded then reading the DataString
can fail to decode the data and return a blank string.
Use the version of TIdHTTP.Post()
that returns a String
and let Indy decode the data for you based on the reply's actual charset:
function PostData(const AURL: string; AParamList: TStrings): string;
var
_idHTTP: TIdHTTP;
begin
_idHTTP := TIdHTTP.Create(nil);
try
_idHTTP.IOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(_idHTTP);
Result := _idHTTP.Post(AURL, AParamList);
finally
FreeAndNil(_idHTTP);
end;
end;
Update: the screenshot you provided shows a GET request, not a POST request:
function GetData(const AURL: string): string;
var
_idHTTP: TIdHTTP;
begin
_idHTTP := TIdHTTP.Create(nil);
try
_idHTTP.IOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(_idHTTP);
Result := _idHTTP.Get(AURL);
finally
FreeAndNil(_idHTTP);
end;
end;
procedure TForm1.BitBtn1Click(Sender: TObject);
var
_Reply: string;
begin
_Reply := GetData('https://eturundus.eu/samsung-pin-lt/_ajax/pin?id=276683492511635&pin=4580');
ShowMessage(_Reply);
end;