function TFormMain.HttpPost(IdHTTP1: TIdHTTP; sJsonData, sUrl: string): string; var jsonToSend: TStringStream; begin try IdHTTP1.HTTPOptions := IdHTTP1.HTTPOptions + [hoKeepOrigProtocol]; //必须有这行才使设置协议版本生效 IdHTTP1.ProtocolVersion := pv1_1;
IdHTTP1.Request.CustomHeaders.Values[':method']:='POST';
IdHTTP1.Request.CustomHeaders.Values[':path']:='/3/device/' + EditDeviceToken.Text;
IdHTTP1.Request.CustomHeaders.Values[':scheme']:='https';
IdHTTP1.Request.CustomHeaders.Values['apns-push-type']:='background';
IdHTTP1.Request.CustomHeaders.Values['host']:='api.push.apple.com';
IdHTTP1.Request.CustomHeaders.Values['apns-topic']:='com.xxvar.erp';
IdSSLIOHandlerSocketOpenSSL1.SSLOptions.CertFile:='d:\WIN\APNS-cert.pem';
IdSSLIOHandlerSocketOpenSSL1.SSLOptions.KeyFile:='d:\WIN\APNS-key.pem';
IdSSLIOHandlerSocketOpenSSL1.ssloptions.method:= sslVSSLv23;
IdSSLIOHandlerSocketOpenSSL1.ssloptions.Mode:= sslmBoth;
with IdHTTP1 do
begin
IOHandler := IdSSLIOHandlerSocketOpenSSL1;
HandleRedirects := True; //允许头转向
ReadTimeout := 5*60*1000; //请求超时设置
Request.ContentType := 'application/json'; //x-www-form-urlencoded
Request.ContentEncoding := 'utf-8';
try
jsonToSend := TStringStream.Create(UTF8Encode(sJsonData));
jsonToSend.Position := 0; //将流位置置为0
Memo1.Lines.Add('发送指令执行结果到集抄平台: ' + sJsonData);
Result:= Post(sUrl, jsonToSend);
Memo1.Lines.Add(Result);//Result := HTTPDecode(Post(sUrl, jsonToSend)); //接收POST后的数据返回
except
on e: Exception do
begin
Memo1.Lines.Add('接口调用异常: ' + e.Message);
jsonToSend.free;
end;
end;
end;
finally end; end;