我使用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