【问题标题】:Getting authentication token after a HttpSendRequest在 HttpSendRequest 之后获取身份验证令牌
【发布时间】:2010-04-21 19:22:58
【问题描述】:

以下代码会将我的应用程序登录到服务器。如果登录成功,该服务器将返回一个身份验证令牌。我需要使用该令牌向服务器查询信息。

  egressMsg := pchar('email='+LabeledEdit1.text+'&&password='+MaskEdit1.Text+#0);
  egressMsg64 := pchar(Encode64(egressMsg));

  Reserved := 0;

  // open connection
    hInternetConn := InternetOpen('MyApp', INTERNET_OPEN_TYPE_PRECONFIG, NIL, NIL, 0);

  if hInternetConn = NIL then
  begin
    ShowMessage('Error opening internet connection');
    exit;
  end;

  // connect
  hHttpSession := InternetConnect(hInternetConn, 'myserver.com',
    INTERNET_DEFAULT_HTTP_PORT, '', '', INTERNET_SERVICE_HTTP, 0, 0);

  if hHttpSession = NIL then
  begin
    ShowMessage('Error connecting');
    exit;
  end;

  // send request
  hHttpRequest := HttpOpenRequest(hHttpSession, 'POST',
      '/myapp/login', NIL, NIL, NIL, 0, 0);

  if hHttpRequest = NIL then
  begin
    ShowMessage('Error opening request');
    exit;
  end;

  label2.caption := egressMsg64 + '    '+inttostr(length(egressMsg64));
  res := HttpSendRequest(hHttpRequest, Nil,
        DWORD(-1), egressMsg64, length(egressMsg64));

  if not res then
  begin
    ShowMessage('Error sending request ' + inttostr(GetLastError));
    exit;
  end;

  BufferSize := Length(infoBuffer);

  res := HttpQueryInfo(hHttpRequest, HTTP_QUERY_STATUS_CODE, @infoBuffer, BufferSize, Reserved);

  if not res then
  begin
    ShowMessage('Error querying request ' + inttostr(GetLastError));
    exit;
  end;

  reply := infoBuffer;
  Memo1.Lines.Add(reply);

  if reply <> '200' then
  begin
    //error here
  end;

  // how to I get the token here!!!!

  InternetCloseHandle(hHttpRequest);
  InternetCloseHandle(hHttpSession);        
  InternetCloseHandle(hInternetConn);

我如何获得该令牌?我尝试查询 cookie,尝试了 InternetGetCookie() 等等。 代码表示赞赏

谢谢

杰斯

编辑

我发现如果您使用 InternetReadFile,您可以获得该令牌。但是,该令牌以字节数组的形式出现。以后很难用它发送到服务器...有人知道如何将字节数组转换为pchar或字符串吗?

【问题讨论】:

    标签: delphi token wininet


    【解决方案1】:

    对于正常的基于零的数组,那不应该是 'Chr(token2[i-1])' 吗?

    或者,假设是非 unicode Delphi,您可以使用:

    SetLength(strtoken, Reserved2);
    CopyMemory(@strtoken[1], @token2[0], Reserved2);
    

    注意:除非您将 strtoken 声明为 AnsiString,否则这将在 Unicode Delphi 中中断
    对于支持 Unicode 的 Delphi,请使用 TEncoding.ASCII.GetString

    【讨论】:

    • 从我发现 token2[0] 里面有数组的大小,所以我呼吁 i:=1 到 Reserved2 做
    • 很公平。但我不禁感到某处潜伏着一个“off by 1”错误。如果您正在读取 Reserved2 字节,则该数组只能保存从索引 [0] 到 [Reserved2 - 1] 的有效值。 token2[Reserved2] 不应包含有用的值,除非 token2 的大小大于 Reserved2,否则应该存在范围检查错误。
    • 我得到一个错误:“常量表达式违反子范围界限”并且光标放在@token2[0]之后
    • 好的,恐怕无法解释。我在我的机器上找到的唯一 InternetReadfile 示例来自我的 TMS 库源代码,它使用您的方法将令牌/缓冲区复制到字符串,但具有 'for i := 0 to Reserved2 - 1 do'...但如果它适合你,然后坚持下去:-)
    【解决方案2】:

    这里是:

      if not InternetReadFile(hHttpRequest, @token2, sizeof(token2), Reserved2) then
        Memo1.Lines.Add('error = ' + inttostr(GetLastError))
      else
      begin
        for i:=1 to Reserved2 do
          strtoken := strtoken + Chr(token2[i]);
    
        Memo1.Lines.Add('token = '+strtoken);
        Memo1.Lines.Add('recevied = '+inttostr(Reserved2));
      end;
    

    token2 是一个字节数组。

    【讨论】:

      猜你喜欢
      • 2015-06-21
      • 2022-09-28
      • 2011-01-07
      • 2019-03-05
      • 1970-01-01
      • 2016-12-07
      • 2012-05-26
      • 2021-06-17
      • 1970-01-01
      相关资源
      最近更新 更多