其他分享
首页 > 其他分享> > delphi get post

delphi get post

作者:互联网

procedure GetDemo;
var
IdHttp : TIdHTTP;
Url : string;//请求地址
ResponseStream : TStringStream; //返回信息
ResponseStr : string;
begin
//创建IDHTTP控件
IdHttp := TIdHTTP.Create(nil);
//TStringStream对象用于保存响应信息
ResponseStream := TStringStream.Create('');
try
//请求地址
Url := 'http://dict.youdao.com/';
try
IdHttp.Get(Url,ResponseStream);
except
on e : Exception do
begin
ShowMessage(e.Message);
end;
end;
//获取网页返回的信息
ResponseStr := ResponseStream.DataString;
//网页中的存在中文时,需要进行UTF8解码
ResponseStr := UTF8Decode(ResponseStr);
finally
IdHttp.Free;
ResponseStream.Free;
end;
end;

procedure PostDemo;
var
IdHttp : TIdHTTP;
Url : string;//请求地址
ResponseStream : TStringStream; //返回信息
ResponseStr : string;

RequestList : TStringList; //请求信息
RequestStream : TStringStream;
begin
//创建IDHTTP控件
IdHttp := TIdHTTP.Create(nil);
//TStringStream对象用于保存响应信息
ResponseStream := TStringStream.Create('');

RequestStream := TStringStream.Create('');
RequestList := TStringList.Create;
try
Url := 'http://f.youdao.com/?path=fanyi&vendor=fanyiinput';
try
//以列表的方式提交参数
RequestList.Add('text=love');
IdHttp.Post(Url,RequestList,ResponseStream);

//以流的方式提交参数
RequestStream.WriteString('text=love');
IdHttp.Post(Url,RequestStream,ResponseStream);
except
on e : Exception do
begin
ShowMessage(e.Message);
end;
end;
//获取网页返回的信息
ResponseStr := ResponseStream.DataString;
//网页中的存在中文时,需要进行UTF8解码
ResponseStr := UTF8Decode(ResponseStr);
finally
IdHttp.Free;
RequestList.Free;
RequestStream.Free;
ResponseStream.Free;
end;
end;

标签:TStringStream,end,get,Url,IdHttp,delphi,ResponseStr,post,ResponseStream
来源: https://www.cnblogs.com/cnapp/p/16434406.html