Solved

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

Posted on 2009-05-18
5
1,770 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Dev Express grid collapse 2 44
windbg taking it's time 32 90
How to create virtual printer while installing my application 5 47
VS2015 Redefinition errors 4 47
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

815 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