【问题标题】:Delphi XE7 Indy Rest Api post with Ssl Connection Timeout 10060带有 SSL 连接超时 10060 的 Delphi XE7 Indy Rest Api 帖子
【发布时间】:2019-01-17 02:03:48
【问题描述】:

我正在尝试在 ssl 地址服务下发帖,但连接超时 10060。我的 ssl 库和 Indy SSl 配置是正确的,因为我在使用 gmail 和其他服务发送电子邮件时使用了相同的代码。

我用邮递员发布它有效。

我的代码

const
  Api = 'https://xxxx.xxxx.com/api/detection/Insert';

procedure TRestSender.SendThreats(CustomerId: Integer;
  DetectionName, Filename: String);
var
  PostData: TStringList;
  res: string;
  Https: TIdHttp;
  IdSSL: TIdSSLIOHandlerSocketOpenSSL;
begin
  Https := Tidhttp.Create(nil);
  PostData := TStringList.Create;
  IdSSL := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
  Https.ReadTimeout := 10000;
  Https.ConnectTimeout:= 10000;

  IdSSL.SSLOptions.Method := sslvTLSv1;
  // IdSSL.OnStatusInfo:= ssl1StatusInfo;
  IdSSL.SSLOptions.Mode := sslmClient;
  Https.IOHandler := IdSSL;
  try
    PostData.Add('Content-Type:application/x-www-form-urlencoded');
    PostData.Add('CustomerId=' + IntToStr(CustomerId));
    PostData.Add('DetectionName=' + DetectionName);
    PostData.Add('DeviceName=' + ComputerName());
    PostData.Add('Filename=' + Filename);
    PostData.Add('ApiUser=' + 'some-code');
    PostData.Add('ApiPass=' + 'some-paswd');
    res := Https.Post(Api, PostData);
  finally
    PostData.Free;
    Https.Free;
    IdSSL.Free;
  end;
end;

【问题讨论】:

  • 附带说明,请勿在您的PostData 中添加Content-Type 条目。该值属于TIdHTTP.Request.ContentType 属性而不是:Https.Request.ContentType := 'application/x-www-form-urlencoded';,实际上您不需要手动执行此操作,因为TIdHTTP.Post()TStrings 版本会在内部为您处理。

标签: delphi ssl post indy idhttp


【解决方案1】:

我有两个建议:

  1. 错误的 TLS 版本:越来越多的服务禁用 TLS 1.0 和/或 TLS1.1。默认版本是 TLS 1.0。

    const
      DEF_SSLVERSION = sslvTLSv1;
      DEF_SSLVERSIONS = [sslvTLSv1];
    

    所以添加以下行:

    IdSSL.SSLOptions.SSLVersions := [sslvTLSv1_2, sslvTLSv1_1, sslvTLSv1];
    
  2. 缺少 SNI 支持 (an example for SNI)。

【讨论】:

  • 感谢您的建议,它适用于 SNI 配置。非常感谢。
  • 如果你使用SSLOptions.SSLVersions,也不要使用SSLOptions.Method(反之亦然)。设置一个更新另一个。另请注意,在[ ] 括号之间指定标志的顺序无关紧要,[sslvTLSv1_2, sslvTLSv1_1, sslvTLSv1][sslvTLSv1, sslvTLSv1_1, sslvTLSv1_2] 是相同的。是的,SNI 已经在最新版本的 Indy 中自动处理。
  • 感谢@remy 的评论实际上我有一个问题,但我不想提出新问题,实际上是关于同一主题。我将 RestClient 用于相同的 Web 服务,但用于不同的项目。当我在 ssl RestClient 下调用服务时出现相同的错误(超时 10600)如何为 ssl post 和 Get 方法配置 RestClient?
  • @JamesFranklin 那是一个不同的问题,与TIdHTTP 无关,所以应该单独发布。请遵循 StackOverflow 对此事的指导。无论如何,我对此没有答案,我不知道 RestClient 在内部是如何工作的,也不知道如何配置其 SSL 设置(如果可能的话)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多