Delphi使用TIdhttp發送Cookie的例子,是模擬發送,當使用TIdHttp執行網絡請求時模擬cookIE值發送到服務器端。思路是先用一個for循環刪除原來的cookie值,然後再使用Request.CustomHeaders.Add方法創建cookIEs:
01
uses StrUtils;
02
procedure TForm1.Button1Click(Sender: TObject);
03
var
04
J: Integer;
05
HtmlStr: string;
06
begin
07
//刪除原有cookIE值
08
for J := IdHTTP1.Request.CustomHeaders.Count - 1 downto 0 do begin
09
if StartsText('cookIE:', IdHTTP1.Request.CustomHeaders[J]) then
10
IdHTTP1.Request.CustomHeaders.Delete(J);
11
end;
12
IdHTTP1.Request.CustomHeaders.Add('CookIE: username=bcoder' ) ;
13
//下面的方法也可實現,不過沒測試過
14
//IdHTTP1.Request.CustomHeaders.Values['cookIE'] := 'username=bcoder';
15
HtmlStr := IdHTTP1.Get('http://www.baidu.com');
16
end;
TIdhttp的一個屬性,AllowCookies,當此值為False時允許用戶發送自定義的cookie,當此值為True時,不允許用戶發送自定義的cookIE。