delphi http post请求后台,获取json数据

unit unHttp;

interface

uses
  superobject;

type
  TCallback = procedure(errMsg: string) of object;
function post(url: string; params: string; callback: TCallback): ISuperObject;

implementation

uses msxml, System.Variants;

function post(url: string; params: string; callback: TCallback): ISuperObject;
var
  req: IXMLHttpRequest;
  jsObj: ISuperObject;
  i: Integer;
begin
  req := CoXMLHTTP.Create;
  req.open('POST', url, false, EmptyParam, EmptyParam);
  req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  req.send(params);
  jsObj := SO(req.responseText);
  if jsObj.i['retid'] = 0 then
  begin
    callback(jsObj.S['retmsg']);
    result := nil;
  end
  else
    result := jsObj.O['data'];
end;

end.

调用示例,中文无须编码,返回结果无须转码,请求后台utf8编码数据

var
  jsRet: ISuperObject;
begin
  jsRet := post('http://xxx.cn','f=order_test1&p=[{"token":"23115683430a3ed168dc4c76d87bbc8f","name":"我是参数"}]',callback);
  if Assigned(jsRet) then  
  Writeln(jsRet.AsString);
end;

 

转载于:https://www.cnblogs.com/lobtao/articles/7490369.html

发布了0 篇原创文章 · 获赞 10 · 访问量 6万+
展开阅读全文
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览