delphi – HTTPS post – 我做错了什么?

我正在向webaddress发出请求,要求通过HTTPS连接获取
XML文件.但这种联系的效果就像50%.在大多数情况下它失败了.通常的错误是“套接字错误#10060”.或者“与SSL连接时出错.观察到违反协议的EOF”.我做错了什么?

function SendRequest(parameters: string): IXMLDocument;
var
   sPostData: TStringList;
   sHttpSocket: TIdHTTP;
   sshSocketHandler: TIdSSLIOHandlerSocketOpenSSL;
   resStream: TStringStream;
   xDoc: IXMLDocument;

   begin
      sPostData := TStringList.Create;

      try
         sPostData.Add('add some parameter to post' + '&');
         sPostData.Add('add some parameter to post' + '&');
         sPostData.Add('add some parameter to post' + '&');
         sPostData.Add(parameters);

         sHttpSocket := TIdHTTP.Create;
         sshSocketHandler := TIdSSLIOHandlerSocketOpenSSL.Create;
         sHttpSocket.IOHandler := sshSocketHandler;

         sHttpSocket.Request.ContentType := 'application/x-www-form-urlencoded';
         sHttpSocket.Request.Method := 'POST';

         resStream := TStringStream.Create;
         sHttpSocket.Post(Self.sUrl, sPostData, resStream);

         xDoc := CreateXMLDoc;
         xDoc.LoadFromStream(resStream);

         Result := xDoc;

         resStream.Free;
         sHttpSocket.Free;
         sshSocketHandler.Free;
         sPostData.Free;
      except on E: Exception do
      begin
         TCommon.ErrorLog('errorLog.txt', DateTimeToStr(Now) + ' ' + E.Message);
      end
      end;
    end;

也许我可以用另一种方式做到这一点,当互联网连接可用时,它的工作方式就像100%一样?

问候,
恶者

最佳答案 “EOF”错误表明您正在连接到实际上并未使用SSL的服务器,或者SSL数据可能已损坏.

除此之外,你为什么要包括明确的’&’你的帖子数据参数之间的字符?不要这样做,Indy只会编码并发送自己的’&’字符.另外,请考虑使用TMemoryStream而不是TStringStream来确保IXMLDocumect.LoadFromStream()按原样加载服务器的原始原始XML数据,而不是由于Unicode处理而导致RTL / VCL生成的更改版本(TStringStream启用了TEncoding).

编辑:根据您提供的URL,调用verifyUser()的示例如下所示:

const
  ERPLYAccountCode = '...';

function verifyUser(const user, pass: string; const sessionLength: Integer = 3600): IXMLDocument;
var
  sPostData: TStringList;
  sHttpSocket: TIdHTTP;
  sshSocketHandler: TIdSSLIOHandlerSocketOpenSSL;
  resStream: TMemoryStream;
  xDoc: IXMLDocument;
begin
  Result := nil;
  try
    resStream := TMemoryStream.Create;
    try
      sPostData := TStringList.Create;
      try
        sPostData.Add('clientCode=' + ERPLYAccountCode);
        sPostData.Add('request=verifyUser');
        sPostData.Add('version=1.0');
        sPostData.Add('responseType=XML');
        sPostData.Add('responseMode=normal');
        sPostData.Add('username=' + user);
        sPostData.Add('password=' + pass);
        sPostData.Add('sessionLength=' + IntToStr(sessionLength));
        sHttpSocket := TIdHTTP.Create;
        try
          sshSocketHandler := TIdSSLIOHandlerSocketOpenSSL.Create(sHttpSocket);
          sHttpSocket.IOHandler := sshSocketHandler;
          sHttpSocket.Request.ContentType := 'application/x-www-form-urlencoded';
          sHttpSocket.Post('https://www.erply.net/api/', sPostData, resStream);
        finally
          sHttpSocket.Free;
        end;
      finally
        sPostData.Free;
      end;
      resStream.Position := 0;
      xDoc := CreateXMLDoc;
      xDoc.LoadFromStream(resStream);
      Result := xDoc;
    finally
      resStream.Free;
    end;
  except
    on E: Exception do
    begin
      TCommon.ErrorLog('errorLog.txt', DateTimeToStr(Now) + ' ' + E.Message);
    end;
  end;
end; 
点赞