delphi实现post请求

2020-08-26

delphi实现post请求,前提头部先引用IDHttp组件

var

fhttpRequest: TIdHTTP;

PostData : TStringStream;

tmpStr : String;

begin

fhttpRequest := TIdHTTP.Create(nil);

  fhttpRequest.HandleRedirects := True;//允许头转向

  fhttpRequest.ReadTimeout := 5000;//请求超时设置

  fhttpRequest.Request.ContentType := 'application/x-www-form-urlencoded';//设置内容类型为json

  PostData := TStringStream.Create('');

  //PostData.Position := 0;//将流位置置为0

  PostData.WriteString('encode=123');

  PostData.WriteString('asd');

  tmpStr := fhttpRequest.Post('http://10.32.8.34:8081/phis/EncodeServlet',PostData); //tmpStr是提交后返回的数据

  fhttpRequest.free;

  PostData.Free;

  Result:=tmpStr;

end;


阅读2472