站点是自己写的站点,想做个客户端的登录窗体结合webbroswer实现登录
wpf如何请求才能访问到站点的session呢?
POST登录后获取httpwebresponse的cookie
webbroswer登录后获取cookie
请求这个地址的时候设置了一个cookie
(地址1)http://localhost:2234/handler/account/login.ashx
返回成功后,跳转地址2的时候cookie获取不到(null)
按理说cookie是保存在本地的,应该能获取到才是啊,但是获取却是null,非常费解
@落幕: 302跳转了是获取不到的,用CookieContainer然后遍历,或者取消自动重定向,自己处理302请求
@梦里的畅泳: 302是什么 没明白
@落幕: 建议你了解下cookie是怎么保存的,比如IE的Cookie是保存到ie特定的目录,chrome是保存到特定的目录。而cookie分为内存cookie和文件cookie,你自己的程序没有持久化到文件的话就是内存cookie
@梦里的畅泳:
public class CookieHelper { /// <summary> /// 清除指定Cookie /// </summary> /// <param name="cookiename">cookiename</param> public static void ClearCookie(string cookiename) { HttpCookie cookie = HttpContext.Current.Request.Cookies[cookiename]; if (cookie != null) { cookie.Expires = DateTime.Now.AddYears(-3); HttpContext.Current.Response.Cookies.Add(cookie); } } /// <summary> /// 获取指定Cookie值 /// </summary> /// <param name="cookiename">cookiename</param> /// <returns></returns> public static string GetCookieValue(string cookiename) { HttpCookie cookie = HttpContext.Current.Request.Cookies[cookiename]; string str = string.Empty; if (cookie != null) { str = cookie.Value; } return str; } /// <summary> /// 添加一个Cookie(24小时过期) /// </summary> /// <param name="cookiename"></param> /// <param name="cookievalue"></param> public static void SetCookie(string cookiename, string cookievalue) { SetCookie(cookiename, cookievalue, DateTime.Now.AddDays(1.0)); } /// <summary> /// 添加一个Cookie /// </summary> /// <param name="cookiename">cookie名</param> /// <param name="cookievalue">cookie值</param> /// <param name="expires">过期时间 DateTime</param> public static void SetCookie(string cookiename, string cookievalue, DateTime expires) { HttpCookie cookie = new HttpCookie(cookiename) { Value = cookievalue, Expires = expires }; HttpContext.Current.Response.Cookies.Add(cookie); } }
@梦里的畅泳: 这是我保存cookie的方法
@梦里的畅泳: 请求地址1的时候保存了一个cookieSetCookie(userid,001)
webbroswer跳转地址2的时候getcookie(userid)=null
@梦里的畅泳: 大神 能帮我看看吗?我现在就这个cookie获取不到的问题,解决的话有红包感谢:qq:2391222329