Request.Cookies和Response.Cookies [英] Request.Cookies and Response.Cookies

查看:70
本文介绍了Request.Cookies和Response.Cookies的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试这个教程:

ASP中的Cookie .NET [ ^ ]



我有2个问题:Request.Cookies和Response.Cookies

请求和响应类在我的代码中不可见。为什么?

我该怎么办?



我读到:这就是关于ASP.NET的所有内容。

我正在使用WindowsFormsApplication。我应该用别的东西吗?

在WindowsFormsApplication中创建这个东西是不可能的?

Whaaaayyyyy?

谢谢。



我想用这个应用程序:

我想为我观看的艺术家制作一个易于查找的软件(我有3页用户,大约200)在deviantart.com上。我在列表中加载每个名字,然后从该列表中选择每个艺术家,并显示他的绘画图像+关于该人的一些信息。

解决方案

Cookies是其中的一部分对于网站的http协议,它们不能从您的Windows窗体应用程序中获得,因为asp.net框架提供了Request\Response对象,允许您的代码在Web请求的上下文中运行。您的表单只是在桌面上运行的EXE。



这是一个如何使用WebClient自行保存cookie的示例。此代码将允许目标站点知道我们是一个返回的vististor,并且任何基于cookie的身份验证或会话ID也将在请求之间保持。



首先关闭要调用的示例页面。如果没有设置此类cookie,则设置包含当前时间的cookie。它还显示输出中cookie的值。在浏览器中查看此页面,它将具有当前时间。保持清爽,时间不会改变。



 <%@     Page    语言  =  C#    AutoEventWireup   =  true    CodeBehind   =  CookieTest.aspx.cs   继承  =  WebApplication1.CookieTest   %>  

< !DOCTYPE html >

< html xmlns = http://www.w3.org/1999/xhtml >
< head runat = server >
< title > < / title >
< / head >
< body >
< 表格 id = form1 runat = server >
< ; div >
<% = Request.Cookies [ TestCookie] == null :Request.Cookies [ TestCookie]。值%>
< / div >
< / form >
< / body >
< / html >





  public   partial   class  CookieTest:System.Web.UI.Page 
{
protected void Page_Load( object sender,EventArgs e)
{
// 如果以前没有此类cookie,此页面设置一个具有当前时间的cookie
// 这样做的效果是时间会反映您第一次查看页面
// 所有后续请求将显示从
// 存储的cookie

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





我打算将示例客户端作为控制台应用程序,但代码也适用于您的winforms应用程序。



公共类CookieAwareWebClient:WebClient 
{
public readonly CookieContainer m_container = new CookieContainer();

protected override WebRequest GetWebRequest(Uri地址)
{
WebRequest request = base.GetWebRequest(address);
HttpWebRequest webRequest =请求为HttpWebRequest;
if(webRequest!= null)
{
webRequest.CookieContainer = m_container;
}
返回请求;
}
}

class程序
{

static void Main(string [] args)
{
//
//首先我们使用标准WebClient发出两个请求
//这不支持cookie所以每个请求都是新的
//我们请求的页面没有知道我们以前要求它
//所以时间会随着每个请求而改变
//

WebClient clientA = new WebClient();

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

Console.WriteLine(**** REQUEST 1使用WEBCLIENT ****);
// html将包含当前时间
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使用WEBCLIENT ****);
// html将包含当前时间
Console.WriteLine(html);

System.Threading.Thread.Sleep(2000);

//
//现在我们使用自定义WebClient发出两个请求
//这确实支持cookie,因此每个请求都会重新发送之前返回的任何cookie
/ /我们现在请求的页面知道我们之前已经请求它
//因此后续请求将显示与第一个请求相同的时间
//

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

Console.WriteLine(**** REQUEST 1使用CUSTOMWEBCLIENT ****);
// html将包含当前时间
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使用CUSTOMWEBCLIENT ****);
// html将包含与前一个示例相同的时间
Console.WriteLine(html);

Console.ReadKey();

}
}


在我的代码中我应该[添加]以使可见>请求回复 CLASSES?

这些课程不可见eeeeeeeeeeeeeeeeeeeeeeeee !!!!!

我不能找到有问题的人...

请帮助!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!

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.

解决方案

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.

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



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();

    }
}


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


这篇关于Request.Cookies和Response.Cookies的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆