Tôi đã triển khai cùng một mã (để đăng biểu mẫu) bằng delphi và python. Mã python hoạt động hoàn hảo, nhưng mã delphi bị lỗi. Trong python, tôi có thể chỉ cần viết httplib2.debuglevel=4
để xem nội dung nào thực sự được gửi đến máy chủ. nhưng tôi không có ý tưởng làm thế nào để in nội dung trong delphi.Cách theo dõi yêu cầu thô và nội dung phản hồi của 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;
Bất kỳ gợi ý nào được đánh giá cao.
Cảm ơn các gợi ý của @bummi. Sau khi nhìn thấy nhật ký, tôi thấy TIdHttp gửi phiên bản giao thức không chính xác, trừ khi tôi đặt Client.HTTPOptions: = [hoKeepOrigProtocol]. – stanleyxu2005