Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2009-05-18
5
Medium Priority
?
1,932 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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 response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

704 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