Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 15th, 2005, 09:37 AM
Registered User
 
Join Date: Mar 2005
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default reading cookies from c# windows app

Hi,

I have two questions:

1) My C# windows application needs to read a cookie produced by a web page. is it
possible to do so? if yes, then how? (the webpage will be different everytime and hence i dont no the name of the cookie!)
any suggestions/ideas/links, etc welcome!

2) My C# windows application needs to check whether user has got a url open in the
browser or not. (for example, it needs to check whether www.google.com is open
in browser on his/her PC and if it is active or inactive!). How can I do that? Any
ideas/suggestions/links, etc welcome!

Sajid
Developer, UK
 
Old April 16th, 2005, 08:27 AM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

about your first question,If you realy don't know the name of cookie then how you want to find it?

just I know

string path=Environment.GetFolderPath(Environment.Special Folder.Cookies);

it gives you the path in which cookies are saved on the local computer..now make a DirectoryInfo and iterate throw all files and find your file(your cookie is changing but it should have something constant in its name)

about the next question

if you wanted just to open a new explorer,

System.Diagnostics.Process.Start("IExplore.exe", "http://www.google.com")

if you wanted to see whether explore is open the only way I know is using P/Invoking (i.e FindWindowEx...)

_____________
Mehdi.
software student.
 
Old April 16th, 2005, 10:46 AM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

Ok.don't use P/Invoking,always .NET provides some wrapper
Code:
System.Diagnostics.Process[] p=System.Diagnostics.Process.GetProcesses();
foreach(System.Diagnostics.Process p1 in p)
    textBox1.Text+=p1.MainWindowTitle+",";

one of them was Google Search: ProcessInfo MSDN - MyIE2 that means ..

_____________
Mehdi.
software student.
 
Old April 16th, 2005, 10:46 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Mehdi,

How do I look at my cookies and the URL in FireFox? Or a better question: How do I look at my cookies and the current URL regardless of the browser I might have open?
 
Old April 17th, 2005, 08:41 AM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

I don't know anything about the URL maybe there is some COM objects,

cookies is located in such places,

string path=Environment.GetFolderPath(Environment.Special Folder.Cookies);
string path=Environment.GetFolderPath(Environment.Special Folder.InternetCache);


you can iterate through all the files in such directories(DirectoryInfo.GetFiles Method)and find the cookie in a way according to its name or something else..without having any info about the cookie I don't know anything.

_____________
Mehdi.
software student.
 
Old April 17th, 2005, 09:09 AM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

From MSDN
Code:
[C#] 
using System.Net;
using System;
namespace Examples.System.Net.Cookies
{
    // This example is run at the command line.
    // Specify one argument: the name of the host to 
    // send the request to.
    // If the request is sucessful, the example displays the contents of the cookies
    // returned by the host.

    public class CookieExample
    {   
        public static void Main(string[] args)
        {   
            if (args == null || args.Length != 1)
            {
                Console.WriteLine("Specify the URL to receive the request.");
                Environment.Exit(1);
            }
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(args[0]);
            request.CookieContainer = new CookieContainer();

            HttpWebResponse response = (HttpWebResponse) request.GetResponse();
            response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);


            // Print the properties of each cookie.
            foreach (Cookie cook in response.Cookies)
            {
                Console.WriteLine("Cookie:");
                Console.WriteLine("{0} = {1}", cook.Name, cook.Value);
                Console.WriteLine("Domain: {0}", cook.Domain);
                Console.WriteLine("Path: {0}", cook.Path);
                Console.WriteLine("Port: {0}", cook.Port);
                Console.WriteLine("Secure: {0}", cook.Secure);

                Console.WriteLine("When issued: {0}", cook.TimeStamp);
                Console.WriteLine("Expires: {0} (expired? {1})", 
                    cook.Expires, cook.Expired);
                Console.WriteLine("Don't save: {0}", cook.Discard);    
                Console.WriteLine("Comment: {0}", cook.Comment);
                Console.WriteLine("Uri for comments: {0}", cook.CommentUri);
                Console.WriteLine("Version: RFC {0}" , cook.Version == 1 ? "2109" : "2965");

                // Show the string representation of the cookie.
                Console.WriteLine ("String: {0}", cook.ToString());
            }
        }
    }
}
_____________
Mehdi.
software student.
 
Old April 17th, 2005, 01:08 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Unfortunately only IE stores its cookies in "Environment.SpecialFolder.Cookies".
 
Old April 17th, 2005, 04:22 PM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

you can search registry to find out at least which explorers is installed..also you can get processes to see which processes is currently active..according to the explorer you can select the folder in which cookies are saved...and if you just need to know some info about the cookie(i.e its value) you can use that MSDN example.

_____________
Mehdi.
software student.





Similar Threads
Thread Thread Starter Forum Replies Last Post
implementing more than one windows in an app helloise C# 4 November 10th, 2008 12:42 PM
Can't Debug Windows App? Ron Howerton Visual Studio 2005 5 November 30th, 2007 05:20 PM
Windows Standalone app echovue Visual Basic 2005 Basics 3 March 12th, 2007 03:27 PM
Accessing Windows service from a windows app sajid08 C# 1 October 6th, 2006 10:25 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.