delphi - 使用 IdHttp Indy 与 SSL 连接时出错

标签 delphi ssl indy delphi-5 idhttp

我在使用 Indy (Delphi) 的 IdHttp 中遇到问题。

我尝试使用 IdHttp 在 Web 服务 SOAP 中发布 XML,但不起作用。返回“使用 SSL 连接时出错”。来自 indy 的 IdSSLOpenSSL.Connect@1437。

我的代码很简单:

procedure TForm1.Button1Click(Sender: TObject);
var
  vRequest : TStringStream;
  s : String;
begin
  vRequest  := TStringStream.Create((Memo1.Lines.Text));
  try
    IdHTTP1.Host                       := edHost.Text;
    IdHTTP1.Request.ContentLength      := length(Memo1.Lines.Text);
    IdHTTP1.Request.ContentType        := edContentType.Text;
    IdHTTP1.Request.CustomHeaders.Text := 'SOAPAction: "removed for safe"'#13#10;
    IdHTTP1.request.CacheControl       := 'no-cache';
    IdHTTP1.Request.AcceptEncoding     := edAccept.Text;
    IdHTTP1.HTTPOptions                := [hoKeepOrigProtocol];
    IdHTTP1.ProtocolVersion            := pv1_1;

    Memo2.Clear;
    try
      s := IdHTTP1.post(Edit1.Text, vRequest);
      Memo2.Lines.Text := s;
    except
      on e: EIdHTTPProtocolException do begin
        Label1.Caption     := e.Message;
        MEMO2.LINES.TEXT   := e.Message;
      end;
      on e:Exception do begin
        Label1.Caption     := e.Message;
        MEMO2.LINES.TEXT   := e.Message;
      end;
    end;

    requestHeaders.Lines.Text  := IdHTTP1.Request.RawHeaders.Text;
    responseHeaders.Lines.Text := IdHTTP1.Response.RawHeaders.Text;
  finally
    vRequest.Free;
  end;
end;

exe文件夹中包含libeay32.dll和ssleay32.dll。有什么建议吗?

我使用delphi 5,但在delphi 7中也是同样的问题。

最佳答案

默认情况下,TIdSSLIOHandlerSockOpenSSLSSLVersions 属性设置为仅启用 TLS 1.0 1。但许多网站开始逐步淘汰 TLS 1.0,只接受 TLS 1.1+。因此,请尝试在 SSLVersions 属性中启用 TLS 1.1 和 TLS 1.2,看看是否有帮助。

1:有一个 open ticket在 Indy 的问题跟踪器中默认启用 TLS 1.1 和 TLS 1.2。


顺便说一句,您应该对代码进行一些进一步的调整:

  • 不要为 TIdHTTPHostContentLength 属性分配任何值。它们由 TIdHTTP 自动填充。

  • 如果您手动设置 AcceptEncoding 属性,请确保不要包含 deflategzip,除非您有 压缩机分配给TIdHTTP,否则它将无法解码压缩响应。除非您准备好处理自定义编码,否则您确实不应该向 AcceptEncoding 分配任何内容。 压缩机处理deflate/gzip,并且TIdHTTP将相应地更新AcceptEncoding,如果压缩机已分配并可供使用。

  • 使用 CustomHeaders.Values 属性设置各个 header ,而不是 CustomHeaders.Text 属性。

  • 您不需要显式捕获 EIdHTTPProtocolException,因为该异常处理程序不会执行更通用的 Exception 处理程序未执行的任何额外操作。

  • RawHeaders 属性是 TStringList 后代,因此使用 Lines.Assign(RawHeaders) 比使用 Lines.Assign(RawHeaders) 更有效Lines.Text := RawHeaders.Text.

试试这个:

procedure TForm1.Button1Click(Sender: TObject);
var
  vRequest : TStringStream;
  s : String;
begin
  IdHTTP1.Request.ContentType := edContentType.Text;
  IdHTTP1.Request.CustomHeaders.Values['SOAPAction'] := 'removed for safe';
  IdHTTP1.Request.CacheControl := 'no-cache';
  IdHTTP1.HTTPOptions := [hoKeepOrigProtocol];
  IdHTTP1.ProtocolVersion := pv1_1;

  Memo2.Clear;
  try
    vRequest := TStringStream.Create(Memo1.Lines.Text);
    try
      s := IdHTTP1.Post(Edit1.Text, vRequest);
    finally
      vRequest.Free;
    end;
    Memo2.Lines.Text := s;
  except
    on e: Exception do begin
      Label1.Caption := e.Message;
      Memo2.Lines.Text := e.Message;
    end;
  end;

  RequestHeaders.Lines.Assign(IdHTTP1.Request.RawHeaders);
  ResponseHeaders.Lines.Assign(IdHTTP1.Response.RawHeaders);
end;

关于delphi - 使用 IdHttp Indy 与 SSL 连接时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50856121/

相关文章:

delphi - 如何在 Delphi 中设置文件的压缩属性?

delphi - 在 Delphi 中将事件监听器分配给动态创建的 TPanel

python - 如何运行 django sslserver(https) 使得网络中的远程系统也可以访问?

android - Rails 3 和 Android 移动应用程序中的自定义 HTTP header 通过 HTTPS 消失

delphi - Indy 将 Memorystream 上传到 FTP

delphi - Indy POP3 将标题放入 TMemo

string - 为什么 TPageProducer 不删除字符串中的引号?

delphi - delphi 无效参数

security - SSLVPN 是否使您的 http 请求加密?

delphi - Indy http 后解码