You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Overbyte ICS中加载PFX证书实现主动POST请求?

解决方案

问题根源

TSslContext.SslCertFile仅支持PEM格式的证书文件,而你的PFX是PKCS#12格式的二进制证书包,直接赋值会导致ICS无法解析,抛出"No PEM Content Found"错误。必须使用ICS提供的LoadFromFileEx方法专门加载PKCS#12证书。

修改后的代码

procedure TMyExampleClass.AddHeaders(Sender: TObject; const Method: String; Headers: TStrings);
begin
  Headers.Add('Ocp-Apim-Subscription-Key: topsecretsubscriptionkey'); 
end;

var
  http: TSslHttpCli;
  ssl: TSslContext;
  request, response: TStringStream;
  errormsg: string;
begin
  http := TSslHttpCli.Create(nil);
  ssl := TSslContext.Create(nil);
  request := TStringStream.Create('', TEncoding.UTF8);
  response := TStringStream.Create('', TEncoding.UTF8);
  try
    request.WriteString(payload);
    request.Position := 0;
    
    // 关键:用LoadFromFileEx加载PKCS#12格式的PFX证书
    if not ssl.LoadFromFileEx('c:\temp\certfile.pfx', croYes, croYes, '', errormsg) then
      raise Exception.Create('加载证书失败: ' + errormsg);

    // 匹配Indy的SSL版本设置
    ssl.SslVersions := [sslvTLSv1_2];

    http.SslContext := ssl;
    http.SslEnabled := True; // 显式启用SSL

    http.Agent := 'MySpecialClient';
    http.ContentTypePost := 'application/json; charset=utf-8';
    http.OnBeforeHeaderSend := AddHeaders;
    http.URL := 'https://myexample.com/v1/api/message';
    http.SendStream := request;
    http.RcvdStream := response;
    http.Post;    

    response.SaveToFile('c:\temp\test.json');
  finally
    response.Free;
    request.Free;
    ssl.Free;
    http.Free;
  end;
end;

关键说明

  • LoadFromFileEx参数解析:
    • 第1个参数:PFX证书文件路径
    • 第2个参数croYes:加载证书到SSL上下文
    • 第3个参数croYes:加载证书关联的私钥
    • 第4个参数:PFX证书的保护密码(如果你的PFX没有密码,传空字符串即可)
    • 第5个参数:错误信息输出,加载失败时会返回具体原因
  • 必须显式设置SslVersions为[sslvTLSv1_2],和之前Indy的配置保持一致,避免SSL版本协商失败
  • 启用SslEnabled := True确保HTTPS请求使用SSL加密通道

内容的提问来源于stack exchange,提问作者complete_stranger

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 19:48:11