我在使用 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中也是同样的问题。
最佳答案
默认情况下,TIdSSLIOHandlerSockOpenSSL
的 SSLVersions
属性设置为仅启用 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。
顺便说一句,您应该对代码进行一些进一步的调整:
不要为
TIdHTTP
的Host
或ContentLength
属性分配任何值。它们由TIdHTTP
自动填充。如果您手动设置
AcceptEncoding
属性,请确保不要包含deflate
或gzip
,除非您有压缩机
分配给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/