怎么在ajax的ashx 文件中获取cookie

xlz923 2011-08-05 09:58:52
在后台页面 中可以使用cookie HttpCookie IPAddressCookie =Request.Cookies["IPAddressCookie"];
IPAddressCookie = new HttpCookie("IPAddressCookie");
IPAddressCookie.Values["IPAddress"] = WebHelper.Pages.Request.GetIP();
IPAddressCookie.Expires = DateTime.Now.AddHours(1);
Response.Cookies.Add(IPAddressCookie);

但是在ashx中提示找不到request.cookie
谁能给个设置cookie的代码例子
...全文
894 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
h305743261 2011-08-05
  • 打赏
  • 举报
回复
IRequiresSessionState
孟子E章 2011-08-05
  • 打赏
  • 举报
回复


public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
context.Response.Write("foo="+context.Request.Cookies["IPAddressCookie"];);
}
x363961461 2011-08-05
  • 打赏
  • 举报
回复

/// <summary>
/// 获取购物车信息
/// </summary>
/// <returns></returns>
public static string GetShoppingInfo() {
HttpCookie hc = null;
hc = HttpContext.Current.Request.Cookies[CookieName];
if (hc == null) //如果Cookie存在 则直接新建Cookie 保存商品id和数量;
{
return null;
}
else {
return hc.Value;
}
}

xlz923 2011-08-05
  • 打赏
  • 举报
回复
楼上的我看过。不过有几个地方不懂,能注释一下么
1HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(requestURL);
requestUrl 要写具体地址么

2 Uri uri = new Uri(ConfigurationHelper.GetAppSettings("Uri_Value"));
这句话代表什么。

3
myRequest.CookieContainer.SetCookies(uri, cookieStr.ToString());
子夜__ 2011-08-05
  • 打赏
  • 举报
回复
HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(requestURL);
myRequest.Method = "post";

//开始提取站点cookie的信息
String[] arr1 = Request.Cookies.AllKeys;
StringBuilder cookieStr = new StringBuilder();
for (int i = 0; i < arr1.Length; i++)
{
HttpCookie hc = Request.Cookies[arr1[i]];
if ((hc.Name != null) && (hc.Value != null))
{
cookieStr.Append(hc.Name + ",");
}
}
if (cookieStr.Length > 1)
{
cookieStr.Remove(cookieStr.Length - 1, 1);
}
Uri uri = new Uri(ConfigurationHelper.GetAppSettings("Uri_Value"));

//CookieContainer使用之前必须new一个,否则是NULL的
myRequest.CookieContainer = new CookieContainer();

//在SetCookie方法中,传入的Cookie是站点cookie的key集合,通过逗号分隔
myRequest.CookieContainer.SetCookies(uri, cookieStr.ToString());
myRequest.Timeout = 300000;
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();

xlz923 2011-08-05
  • 打赏
  • 举报
回复
HttpCookie IPAddressCookie = System.Web.HttpContext.Current.Request.Cookies["IPAddressCookie"];这样也不行的。。
hebaobao19880921 2011-08-05
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 net_lover 的回复:]
context.Request
[/Quote]试试
honkerhero 2011-08-05
  • 打赏
  • 举报
回复
ashx里是不是传递了httpcontex或httpapplication类似的参数啊,它们上边有属性可以取到Request对象,或者HttpContext.Current.Requst, 后台页面的Request是它的一个属性,它的类型是HttpRequest吧
孟子E章 2011-08-05
  • 打赏
  • 举报
回复
context.Request

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧