使用IdHttp Indy连接SSL时出错

时间:2018-06-14 11:08:06

标签: 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中是同样的问题。

1 个答案:

答案 0 :(得分:4)

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

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

另外,您应该对代码进行一些进一步的调整:

  • 不要为SSLVersions的{​​{1}}或TIdHTTP属性分配任何值。它们由Host自动填充。

  • 如果您手动设置ContentLength属性,请确保不要包含TIdHTTPAcceptEncoding,除非您deflate已分配到gzip否则它将无法解码压缩响应。除非您准备处理自定义编码,否则您真的不应该为Compressor分配任何内容。 TIdHTTP句柄AcceptEncoding / Compressordeflate会相应更新gzip,如果TIdHTTP已分配并可以使用。

  • 使用AcceptEncoding属性设置单个标头,而不是Compressor属性。

  • 您不需要显式捕获CustomHeaders.Values,因为该异常处理程序没有执行更通用的CustomHeaders.Text处理程序不执行的任何操作。

  • EIdHTTPProtocolException属性是Exception后代,因此使用RawHeaders代替TStringList效率更高。

试试这个:

Lines.Assign(RawHeaders)
相关问题