Solved

How do you get the session ID from TWebBrowser component in Delphi?

Posted on 2009-05-18
5
1,757 Views
Last Modified: 2013-12-03
I an using TWebBrowser to connect to a web site. I would like to retreive the session ID from the component. How can this be done?
0
Comment
Question by:Code2009
  • 2
5 Comments
 
LVL 26

Expert Comment

by:EddieShipman
ID: 24413373
Usually SessionID is stored in a cookie.
0
 

Author Comment

by:Code2009
ID: 24418533
how do i know where twebbrowser stores the cookie and identify the one that is used for this session?
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 24420973
I'm still researching.
0
 
LVL 1

Accepted Solution

by:
dimitrio earned 500 total points
ID: 24943350
Here's the code to get the session ID from a TWebBrowser object:

(Add Mshtml to the uses section for IHTMLDocument2)

procedure split(const Delimiter: Char; Input: string; const Strings: TStrings);
begin
  Assert(Assigned(Strings));
  Strings.StrictDelimiter := true;
  Strings.Clear;
  Strings.Delimiter := Delimiter;
  Strings.DelimitedText := Input;
end;

function get_cookie_value(name: String; cookies: String): String;
var
  list1, list2: TStringList;
  i: integer;
begin
  list1 := TStringList.Create;
  list2 := TStringList.Create;
  try
    split(';', cookies, list1);
    for i := 0 to list1.Count-1 do begin
      split('=', list1[i], list2);
      if AnsiCompareText(list2[0], name) = 0 then begin
        Result := list2[1];
        Exit;
      end;
    end;
  finally
  end;
  Result := '';
end;

procedure TForm1.NavigateAndGetSessionID(Sender: TObject);
var
  document: IHTMLDocument2;
  cookies: String;
  session_id: string;
begin
  WebBrowser1.Navigate('http://localhost/autenticar.php');

  while (WebBrowser1.Busy OR (WebBrowser1.ReadyState <> 4)) do
  begin
    Application.ProcessMessages;
  end;

  document := WebBrowser1.Document as IHTMLDocument2;
  if Assigned(document) then begin
    cookies := document.cookie;
    session_id := get_cookie_value('PHPSESSID', cookies);
    ShowMessage('session: ' + session_id);
  end;

end;

There's one thing, you'll need to know what is the session variable name used by the server you're querying. That's because the server can use any name for a session variable, the browser will just store and retrieve all cookies when asked. It's the server's job to identify the user based on data received.

In this example, I've used PHP, so the session variable name is PHPSESSID. You'll need to change that accordingly. If you don't know what's the session variable's name, try ShowMessage(cookies) and you'll see all the cookies available, hopefully your session variable as well, if it's set.

Regards,
Marcos.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now