Solved

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

Posted on 2009-05-18
5
1,728 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
Comment Utility
Usually SessionID is stored in a cookie.
0
 

Author Comment

by:Code2009
Comment Utility
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
Comment Utility
I'm still researching.
0
 
LVL 1

Accepted Solution

by:
dimitrio earned 500 total points
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

772 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

12 Experts available now in Live!

Get 1:1 Help Now