我使用delphi和python实现了相同的代码(发布表单)。 Python代码完美工作,但delphi代码失败。在python中,我可以简单地编写httplib2.debuglevel=4
来查看实际发送到服务器的内容。但我不知道如何在delphi中打印内容。如何跟踪原始请求和响应内容TIdHttp
def python_request_data(url, cookie, data):
httplib2.debuglevel = 4
conn = httplib2.Http()
conn.follow_all_redirects = True
headers = {'Cookie': cookie, 'Content-Type': 'application/x-www-form-urlencoded'}
response, contents = conn.request(url, 'POST', data, headers=headers)
procedure DelphiRequestData(const Url, Cookie, Data: string);
var
Client: TIdHttp;
Params: TStringList;
Response: string;
begin
Client := TIdHttp.Create(nil);
try
Client.HTTPOptions := [hoKeepOrigProtocol];
Client.Request.CustomHeaders.AddValue('Cookie', Cookie);
Params := TStringList.Create;
try
Params.QuoteChar := #0;
Params.Delimiter := '&';
Params.DelimiterText := Data;
Client.Request.ContentType := 'application/x-www-form-urlencoded';
Client.Request.ContentLength := Length(Params.DelimitedText);
Response := Client.Post(Url, Params);
finally
Params.Free;
end;
finally
Client.Free;
end;
end;
任何提示表示赞赏。
感谢@bummi的提示。看到日志后,我看到TIdHttp发送的协议版本不正确,除非我设置Client.HTTPOptions:= [hoKeepOrigProtocol]。 – stanleyxu2005