http get方法

来源:互联网 发布:什么是响应式编程 编辑:程序博客网 时间:2024/06/07 02:44
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;
原创粉丝点击