【发布时间】:2016-01-26 16:55:26
【问题描述】:
我正在使用带有 IdHTTP 库的 Delphi 从该 URL (http://apply.dataprocessors.com.au) 获取表单,处理难题并发布正确答案。但是,显然我没有正确处理 POST 方法,一旦检索到的响应是:“提交问题。请重试。”
下面是Delphi源码:
procedure TForm1.Button1Click(Sender: TObject);
var
sGET, sPOST, sGET_count: String;
countCircles: integer;
parameters: TStringList;
begin
parameters := TStringList.Create;
sGET := http.Get('http://apply.dataprocessors.com.au');
sGET_count := StringReplace(sGET, '$', ' ', [rfReplaceAll, rfIgnoreCase]);
sGET_count := StringReplace(sGET_count, 'unfilled', '$', [rfReplaceAll, rfIgnoreCase]);
countCircles := 120 - sGET_count.CountChar('$');
parameters.Add('title=submit');
parameters.Add('value=' + countCircles.ToString());
parameters.Add('submit=Submit');
http.Request.ContentType := 'application/x-www-form-urlencoded';
sPOST := http.Post('http://apply.dataprocessors.com.au/?', parameters);
txtPost.Lines.Add(sPOST);
end;
HTML 代码:
<html>
<head>
</head>
<body>
<form action="" method="POST">
<input type="hidden" name="title" value="submit">
<p>How many filled circles do you see:? <br><img src="unfilled_O.gif" height="12" width="12"><br></p>
<p>Answer: <input type="text" name="value" value="" size="10"></p>
<p><input type="Submit" value="Submit">
</form></body>
</html>
从 POST 方法检索到的响应:
<html>
<head>
</head>
<body>
<p>Problem with submission. Please try again.</p></body>
</html>
【问题讨论】:
-
使用 HTTP 代理来捕获您的浏览器通信并查看“工作”请求的样子
-
你有什么推荐的程序吗?
-
不需要,在任何现代浏览器中使用 F12 功能(或使用 firebug 和 firefox)并检查网络请求。
-
好像没有设置
TIdHttp.Request.ContentType(应该是'application/x-www-form-urlencoded')。另请参阅此答案stackoverflow.com/a/7769356/1744164 -
@SirRufo:
TIdHTTP.Post()的TStrings版本自动将Request.ContentType设置为application/x-www.webform-urlencoded(如果尚未设置)。