 |
| 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
|
|
|
|

April 15th, 2005, 09:37 AM
|
|
Registered User
|
|
Join Date: Mar 2005
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

April 16th, 2005, 08:27 AM
|
|
Friend of Wrox
|
|
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
|
|
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.
|
|

April 16th, 2005, 10:46 AM
|
|
Friend of Wrox
|
|
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
|
|
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.
|
|

April 16th, 2005, 10:46 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
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?
|
|

April 17th, 2005, 08:41 AM
|
|
Friend of Wrox
|
|
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
|
|
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.
|
|

April 17th, 2005, 09:09 AM
|
|
Friend of Wrox
|
|
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
|
|
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.
|
|

April 17th, 2005, 01:08 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
Unfortunately only IE stores its cookies in "Environment.SpecialFolder.Cookies".
|
|

April 17th, 2005, 04:22 PM
|
|
Friend of Wrox
|
|
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
|
|
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.
|
|
 |