Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to do this tutorial:
Cookies in ASP.NET[^]

I have 2 problems there: Request.Cookies and Response.Cookies
The Request and Response classes are not visible in my code. WHY?
What should i do?

I read : "that is all about the ASP.NET cookies."
I am using WindowsFormsApplication. I should use something else?
It is not Possible to create this things in WindowsFormsApplication?
Whaaaayyyyy?
Thanks.

what i want with this app:
I want to make a easy to find software for my artists that I watch(and I have 3 pages with users,approx 200) on deviantart.com. I load every name in a list, and from that list I select each artist, and his paintings images are shown + some information about the guy.
Posted
Updated 20-Mar-15 18:18pm
v4
Comments
Joan Magnet 20-Mar-15 7:10am    
Cookies are used by your web browser to store temporary or permanent data. Is your winform app acting as a web browser?
_Q12_ 20-Mar-15 7:16am    
my app, is gathering data from a website, - images and some text info.
It is not a web browser. I want to be able to login to the website, to check the "mature option" -its about art stuff- deviantart.com is the website.

Cookies are part of the http protocol for websites, they are not available from your windows forms app as the Request\Response objects are provided by the asp.net framework which allows your code to run inside the context of a web request. Your form is just an EXE running on the desktop.

Here is an example of how you can persists cookies yourself using WebClient. This code will allow the target site to know we are a return vististor, and any cookie-based authentication or session ids will persist between requests as well.

First off a sample page to call. This sets a cookie containing the current time if no such cookie has been set. It also shows the value of the cookie in the output. View this page in the browser and it'll have the current time. Keep refreshing and the time doesn't change.

XML
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CookieTest.aspx.cs" Inherits="WebApplication1.CookieTest" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <%= Request.Cookies["TestCookie"] == null ? "" : Request.Cookies["TestCookie"].Value %>
    </div>
    </form>
</body>
</html>


C#
public partial class CookieTest : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // This page sets a cookie that has the current time if no such cookie previously exists
        // the effect of this is that the time will reflect the first time you view the page
        // all subsequent requests will show the same time as that time is being read from the
        // stored cookie

        if (Request.Cookies["TestCookie"] == null)
        {
            Response.Cookies.Add(new HttpCookie("TestCookie", DateTime.Now.ToString()));
        }
    }
}


I'm going to do the sample client as a console app, but the code will work for your winforms app too.

public class CookieAwareWebClient : WebClient
{
    public readonly CookieContainer m_container = new CookieContainer();

    protected override WebRequest GetWebRequest(Uri address)
    {
        WebRequest request = base.GetWebRequest(address);
        HttpWebRequest webRequest = request as HttpWebRequest;
        if (webRequest != null)
        {
            webRequest.CookieContainer = m_container;
        }
        return request;
    }
}

class Program
{

    static void Main(string[] args)
    {
        //
        // First we make two requests using the standard WebClient
        // This doesn't support cookies so every request is new
        // the page we're requesting doesn't know we have requested it previously
        // so the time will change with every request
        //

        WebClient clientA = new WebClient();

        StreamReader r = new StreamReader(clientA.OpenRead("http://localhost:59466/CookieTest.aspx"));
        string html = r.ReadToEnd();

        Console.WriteLine("**** REQUEST 1 USING WEBCLIENT ****");
        // the html will contain the current time
        Console.WriteLine(html);

        System.Threading.Thread.Sleep(2000);

        r = new StreamReader(clientA.OpenRead("http://localhost:59466/CookieTest.aspx"));
        html = r.ReadToEnd();

        Console.WriteLine("**** REQUEST 2 USING WEBCLIENT ****");
        // the html will contain the current time
        Console.WriteLine(html);

        System.Threading.Thread.Sleep(2000);

        //
        // Now we make two requests using the custom WebClient
        // This does support cookies so every request re-sends any cookies previously returned
        // the page we're requesting now knows we have requested it previously
        // so subsequent requests will all show the same time as the first request
        //

        CookieAwareWebClient clientB = new CookieAwareWebClient();
        r = new StreamReader(clientB.OpenRead("http://localhost:59466/CookieTest.aspx"));
        html = r.ReadToEnd();

        Console.WriteLine("**** REQUEST 1 USING CUSTOMWEBCLIENT ****");
        // the html will contain the current time
        Console.WriteLine(html);

        System.Threading.Thread.Sleep(2000);

        r = new StreamReader(clientB.OpenRead("http://localhost:59466/CookieTest.aspx"));
        html = r.ReadToEnd();

        Console.WriteLine("**** REQUEST 2 USING CUSTOMWEBCLIENT ****");
        // the html will contain the same time as in the previous example
        Console.WriteLine(html);

        Console.ReadKey();

    }
}
 
Share this answer
 
v2
Comments
_Q12_ 20-Mar-15 7:17am    
my app, is gathering data from a website, - images and some text info.
It is not a web browser. I want to be able to login to the website, to check the "mature option" -its about art stuff- deviantart.com is the website.
F-ES Sitecore 20-Mar-15 7:24am    
If your app is acting like a browser then it will need to keep the cookies sent in the response from the website, and re-send those cookies every time you make subsequent requests. How you do this depends on what you're using to make the requests, but if you look into CookieContainer objects it might help

https://msdn.microsoft.com/en-gb/library/dd920298(v=vs.95).aspx

http://stackoverflow.com/questions/1777221/using-cookiecontainer-with-webclient-class

Or you can just do it basic....read the cookies from the header, store as text, and just include those cookies via the header when you next make a request. Classes like WebClient, WebRequest etc let you supply your own headers.
_Q12_ 20-Mar-15 7:45am    
really this is new for me- can you give me an inspiration, about how to do it basic? I like it more like that.
thanks
[no name] 20-Mar-15 8:07am    
Hey, there you are again :)
Take a look here: https://msdn.microsoft.com/en-us/library/dd920298%28v=vs.95%29.aspx
Recall you already have the required cookie (if you found it in your browsers cookie cache) so you only have to take care about sending it, not receiving any.
_Q12_ 21-Mar-15 4:42am    
what language are you using?
c# winforms? c# asp.net? what?
I am using c# winforms !!!!!! -that is the reason that i can not run it properly. Cookies are not for winapp... this is the sad truth.
WHAT SHOULD I [ADD] IN MY CODE TO MAKE VISIBLE THE Request and Response CLASSES?
These classes ARE NOT VISIBLEEeeeeeeeeeeeeeeeeeeeee!!!!!
I can not find what is the probleeeeeeeeem...
please heelp!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 
Share this answer
 
v2
Comments
_Q12_ 21-Mar-15 3:03am    
Now you are using .NET Framework 4.
maybe is a .NET problem -try using a Framework 3.5 .

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900