Http POST 的使用

1 篇文章 / 0 new
author
Http POST 的使用
var
    param: TStringList;//for post
    http: TIdHTTP;
    recData:TStringStream;
    responseData:String
begin
    http := TIdHTTP.Create(nil);
    http.HandleRedirects := true;
    http.ReadTimeout := 5000;
    http.Request.ContentEncoding := 'UTF-8';
    param := TStringList.Create;
    param.Clear;
    param.Add('para1=1');
    param.Add('para2=中文');
    try
        //方式一 stream 接收方式
        recData := TStringStream.Create('', TEncoding.UTF8);//須藉此承接訊息不然中文會亂碼
        http.Post('http://localhost/dborm/t.php', param, recData);
        //http.Get('http://localhost/dborm/t.php', recData);
        responseData := recData.DataString;
        {//方式二, 回傳方式
        http.Response.CharSet := 'UTF-8';//指定接收格式
        responseData := http.post('http://localhost/dborm/t.php', param);
        //responseData := http.get('http://localhost/dborm/t.php');}
    except on E: Exception do
    end;
    param.Free;
    recData.Free;
end;
► server端 php
<!DOCTYPE html>
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>中文</title>
</head><body>
<?php
echo 'GET '; print_r($_GET);
echo 'POST '; print_r($_POST);
?>
</body></html>
Free Web Hosting