Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2009-05-18
5
Medium Priority
?
1,990 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 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
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…

972 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