delphi实现post请求,前提头部先引用IDHttp组件
ContentType := ‘application/x-www-form-urlencoded’;
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=773130f7236b15405c9497c19bf24a038ba04197f017bfb0dd66d455bcb75e9e580');
PostData.WriteString('302a5d177cae445ae6c97be24a74344c2cade3dee0015cda9e3ef05a71a9b32409f7d026e633a8125d8d62298aa1a5d35c50e40dc316a07b355d4f70b712b73f37befe911824a78ec556556951d2bf76ec4944e73cbc3b2b88ada31a85b09');
tmpStr := fhttpRequest.Post('http://10.32.8.34:8081/phis/EncodeServlet',PostData); //tmpStr是提交后返回的数据
fhttpRequest.free;
PostData.Free;
Result:=tmpStr;
end;
ContentType := ‘application/json’;
Delphi source:
http := TIdHttp.Create(nil);
http.HandleRedirects := True;//允许头转向
http.ReadTimeout := 5000;//请求超时设置
http.Request.ContentType := 'application/json';//设置内容类型为json
//http.Request.CustomHeaders.Values['Authorization'] := Concat('Bearer ',这里写token信息 );
jsonToSend := TStringStream.Create('{"name":"Peter Pan"}');//创建一个包含JSON数据的变量
jsonToSend.Position := 0;//将流位置置为0
Memo1.Lines.Text := http.Post('http://www.website.com/test.php', jsonToSend);//用MEMO控件接收POST后的数据返回
jsonToSend.free; http.free;//用完记得释放