【问题标题】:How to make an HTTPS POST request in Delphi?如何在 Delphi 中发出 HTTPS POST 请求?
【发布时间】:2011-05-12 13:13:07
【问题描述】:

在 Delphi 中执行 HTTPS POST 请求的最简单方法是什么?我在发出 HTTP POST 请求时没有遇到问题,但是如何使用 SSL 来完成呢?我在 Google 上四处搜索,但没有找到任何可以很好地解释这一点的东西。

这是我尝试过的代码:

procedure TForm1.FormCreate(Sender: TObject);
var
  responseXML:TMemoryStream;
  responseFromServer:string;
begin
  responseXML := TMemoryStream.Create;
  IdSSLIOHandlerSocketOpenSSL1 := TIdSSLIOHandlerSocketOpenSSL.Create(self);
  with idSSLIOHandlerSocketOpenSSL1 do
    begin
      SSLOptions.Method := sslvSSLv2;
      SSLOptions.Mode := sslmUnassigned;
      SSLOptions.VerifyMode := [];
      SSLOptions.VerifyDepth := 0;
      host := '';
    end;

  IdHTTP1 := TIdHTTP.Create(Self);
  with IdHTTP1 do
    begin
      IOHandler := IdSSLIOHandlerSocketOpenSSL1;
      AllowCookies := True;
      ProxyParams.BasicAuthentication := False;
      ProxyParams.ProxyPort := 0;
      Request.ContentLength := -1;
      Request.ContentRangeEnd := 0;
      Request.ContentRangeStart := 0;
      Request.Accept := 'text/html, */*';
      Request.BasicAuthentication := False;
      Request.UserAgent := 'Mozilla/3.0 (compatible; Indy Library)';
      HTTPOptions := [hoForceEncodeParams];
    end;
  responsefromserver := IdHTTP1.Post('https://.../','name1=value1&name2=value2&....');
end;

当我尝试运行它时,我收到以下错误:

Project myProject.exe raised exception class EFOpenError with message 'Cannot open file "C:\...\Projects\Debug\Win32\name1=value1name2=value2 The system cannot find the file specified'.

我不明白。我发送了参数,虽然错误听起来像是我会发送一个文件。

我还在 myProject.exe 文件夹中包含了 libeay32.dll 和 ssleay32.dll。

【问题讨论】:

  • 你找到解决方法了吗?

标签: delphi indy


【解决方案1】:

您没有指定您的 Delphi 版本或 indy 版本,但我之前在使用 Delphi 2009 和 HTTPS 捆绑的 Indy 时遇到了一些问题,当我从 indy svn 获得最新源时,问题解决了。

【讨论】:

    【解决方案2】:

    http://chee-yang.blogspot.com/2008/03/using-indy-https-client-to-consume.html

    var S: TStringList;
       M: TStream;
    begin
     S := TStringList.Create;
     M := TMemoryStream.Create;
     try
       S.Values['Email'] := 'your google account';
       S.Values['Passwd'] := 'your password';
       S.Values['source'] := 'estream-sqloffice-1.1.1.1';
       S.Values['service'] := 'cl';
    
       IdHTTP1.IOHandler := IdSSLIOHandlerSocketOpenSSL1;
       IdHTTP1.Request.ContentType := 'application/x-www-form-urlencoded';
       IdHTTP1.Post('https://www.google.com/accounts/ClientLogin', S, M);
       Memo1.Lines.Add(Format('Response Code: %d', [IdHTTP1.ResponseCode]));
       Memo1.Lines.Add(Format('Response Text: %s', [IdHTTP1.ResponseText]));
    
       M.Position := 0;
       S.LoadFromStream(M);
       Memo1.Lines.AddStrings(S);
     finally
       S.Free;
       M.Free;
     end;
    

    结束;

    【讨论】:

      【解决方案3】:

      Indy 的另一个替代品是Synapse

      这个类库提供了对 post 的完全控制,但也提供了一个简单的单行 post 方法:

      function HttpPostURL(const URL, URLData: string; const Data: TStream): Boolean;
      

      【讨论】:

      • 你的HttpPostURL函数支持https吗?
      猜你喜欢
      • 1970-01-01
      • 2011-05-14
      • 2018-03-26
      • 2011-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-14
      相关资源
      最近更新 更多