「IndyのTIdHTTPでPOSTする」
TIdHTTPでPOSTするにはどうすればよいか。
回答
TIdHTTPでPOSTするデータは、TStringStreamで使用する文字エンコードを指定して作成する。
その時に、URLエンコードを行う必要がある。
usesに
IdBaseComponent,IdComponent, IdTCPConnection,
IdTCPClient, IdHTTP, IdURI, IdGlobal
を追加し、フォームにボタンを配置し、ダブルクリックしてクリック時のソースに以下を入れる。
ソース
procedure TForm1.Button1Click(Sender: TObject);
var poststring:TStringList;
strm:TStringStream;
st:string;
IdHTTP1: TIdHTTP;
begin
IdHTTP1:=TIdHTTP.Create(self);
poststring:=TStringList.Create;
strm:=TStringStream.Create('',TEncoding.UTF8);
st:='ポストする文字列';
st:=TIdURI.ParamsEncode('param='+st,IndytextEncoding_UTF8);
poststring.Add(st);
IdHTTP1.HTTPOptions:=IdHTTP1.HTTPOptions-[TIdHTTPOption.hoForceEncodeParams];
IdHTTP1.Request.ContentType:='appllication/x-www-form-urlencoded';
IdHTTP1.Request.CharSet:='uft-8';
IdHTTP1.Post('http://mam-mam.net/',poststring,strm);
strm.Position:=0;
st:=strm.ReadString(strm.Size);
showmessage(st);
IdHTTP1.DisposeOf;
poststring.DisposeOf;
strm.DisposeOf;
end;