Getting authentication token after a HttpSendRequest

Posted by Jessica on Stack Overflow See other posts from Stack Overflow or by Jessica
Published on 2010-04-21T19:22:58Z Indexed on 2010/04/22 13:43 UTC
Read the original article Hit count: 670

Filed under:
|
|

The following code will log in my application to a server. That server will return an authentication token if the login is successful. I need to use that token to query the server for information.

  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 querrying 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);

How do I get that token? I tried querying the cookie, I tried InternetGetCookie() and a lot more. Code is appreciated

Thanks

jess

EDIT

I found that if you use InternetReadFile you can get that token. However that token comes out as an array of bytes. It's hard to use it later to send it to the server... anyone knows how to convert an array of bytes to pchar or string?

© Stack Overflow or respective owner

Related posts about delphi

Related posts about wininet