我正在尝试做一些帖子。
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;
我应该得到{ "status":"lose","tries":2 }
或{ "status":"over" }
等,但我得到的只是一个消息框中的空字符串。我缺少什么?
编辑: 我添加了
_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';
但仍然是空字符串。
Tamper Data也为我提供了:
<?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>
答案 0 :(得分:3)
根本不要使用TStringStream
。您正在使用它强制执行UTF-8解码,但如果回复数据不是UTF-8编码,则读取DataString
可能无法解码数据并返回空字符串。
使用返回TIdHTTP.Post()
的{{1}}版本,让Indy根据回复的实际字符集为您解码数据:
String
更新:您提供的屏幕截图显示了GET请求,而不是POST请求:
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;