Tag: cookies

通过HttpRequest登录Windows Live ID

我希望通过HttpRequest / HttpResponse与一个网站进行交互。 但是网络需要通过Windows Live ID登录。 我转到实时登录页面,但我无法从登录页面获得任何可用的响应。 用于访问登录页面的代码: var request = (HttpWebRequest)WebRequest.Create(“http://login.live.com/”); request.Timeout = 30000; request.UserAgent = “Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.8) Gecko/20100202 Firefox/3.5.8 (.NET CLR 3.5.30729)”; request.Accept = “text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8”; request.Headers.Add(“Accept-Language”, “en-us,en;q=0.5”); request.Headers.Add(“Accept-Charset”, “ISO-8859-1,utf-8;q=0.7,*;q=0.7”); request.Headers.Add(“Keep-Alive”, “300”); request.ContentType = “application/x-www-form-urlencoded”; request.CookieContainer = new CookieContainer(); request.Method = WebRequestMethods.Http.Get; var response = (HttpWebResponse)request.GetResponse(); 并回应: 必须允许Cookie […]

GetCookie将信息提取到String

我正试图通过Set-Cookie从我得到的cookie获取数字信息我需要&om=-&lv=1341532178340&xrs=这里的数字 这就是我提出的: string key = “”; ArrayList list = new ArrayList(); foreach (Cookie cookieValue in agent.LastResponse.Cookies) { list.Add(cookieValue); } String[] myArr = (String[])list.ToArray(typeof(string)); foreach (string i in myArr) { // Here we call Regex.Match. Match match = Regex.Match(i, @”&lv=(.*)&xrs=”, RegexOptions.IgnoreCase); // Here we check the Match instance. if (match.Success) { // Finally, we get the […]

响应未解析的HttpWebRequest和Set-Cookie标头(WP7)

我试图获取标题“Set-Cookie”或访问cookie容器,但Set-Cookie标头不可用。 cookie位于响应头中,但它不在客户端请求对象中。 我正在使用注册ClientHttp堆栈 bool httpResult = WebRequest.RegisterPrefix(“http://”, WebRequestCreator.ClientHttp); 这是回复: HTTP/1.1 200 OK Content-Type: application/xml; charset=utf-8 Connection: keep-alive Status: 200 X-Powered-By: Phusion Passenger (mod_rails/mod_rack) 3.0.0.pre4 ETag: “39030a9c5a45a24e485e4d2fb06c6389” Client-Version: 312, 105, 0, 0 X-Runtime: 44 Content-Length: 1232 Set-Cookie: _CWFServer_session=[This is the session data]; path=/; HttpOnly Cache-Control: private, max-age=0, must-revalidate Server: nginx/0.7.67 + Phusion Passenger 3.0.0.pre4 (mod_rails/mod_rack) … […]

使.NET WebBrowser不与IE或其他实例共享cookie

由于C#中的WebBrowser与包括IE在内的所有其他WebBrowsers实例共享cookie,我希望WebBrowser拥有自己的cookie容器,该容器不共享之前在IE或其他实例中创建的任何cookie。 因此,例如,当我创建WebBrowser时,它不应该有任何cookie。 当我运行2个WebBrowsers实例时,它们拥有自己的cookie容器,并且不会彼此共享或冲突cookie。 我怎样才能做到这一点?

使用.net(C#)System.Net.Cookie处理cookie值中的逗号

我正在创建一个访问网站并登录的客户端+自动执行某些任务,但是他们最近更新了他们的cookie(无论出于何种原因……)在其标识cookie中包含逗号。 例如,Cookie将具有与此类似的值: a,bcdefghijklmnop 问题是,根据msdn,你不能在cookie的值内使用逗号或句点。 我正在寻找的方法是绕过这个限制,某种方式使.net的Cookie与逗号的工作很好。 我发现服务器确实向客户端发送了一个’SET-COOKIE’标题,我猜这是正在解析的内容,但这似乎也显然给commans和分号带来了特殊的意义(因此限制了.NET内部的类)。 但那么IE,Firefox等浏览器如何正确处理cookie(正如他们显然那样,因为网站在我测试过的任何浏览器中都能正常工作。)是否有办法强迫这个在.NET中的行为? 任何帮助将不胜感激,谢谢。 —编辑— 一些额外的信息: 我的代码看起来像这样: request = (HttpWebRequest)WebRequest.Create(URI); request.CookieContainer = Program.client.cookieJar; 其中cookieJar在Program.client中定义为: CookieContainer cookieJar = new CookieContainer(); 当我遍历并打印出CookieContainer中的所有Cookie时,我得到以下内容:( cookies,格式为:“name” – >“value”) “normal_cookie” -> “i am the value” “messedup_cookie” -> “a” “bcdefghijklmnop” -> “” // What I should get is this: “normal_cookie” -> “i am the value” “messedup_cookie” -> “a,bcdefghijklmnop” […]

如何将cookie传递给HtmlAgilityPack或WebClient?

我使用此代码登录: CookieCollection cookies = new CookieCollection(); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(“example.com”); request.CookieContainer = new CookieContainer(); request.CookieContainer.Add(cookies); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); cookies = response.Cookies; string getUrl = “example.com”; string postData = String.Format(“my parameters”); HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create(getUrl); getRequest.CookieContainer = new CookieContainer(); getRequest.CookieContainer.Add(cookies); getRequest.Method = WebRequestMethods.Http.Post; getRequest.UserAgent = “Mozilla/5.0 (Windows NT 6.2; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0”; getRequest.AllowWriteStreamBuffering = […]

Cookie总是过期的

我正在设置一个cookie: HttpCookie cookie = new HttpCookie(“simpleorder”); cookie.Expires = DateTime.Now.AddYears(1); cookie[“order”] = carModel.ToString(); cookie[“price”] = price.ToString(); Response.Cookies.Add(cookie); 但是当我几秒钟检查它时它已经过期,并且到期日期设置为{01-01-0001 00:00:00}。 我尝试通过检索代码 HttpCookie cookie = Request.Cookies[“simpleorder”]; if (cookie != null && cookie.Expires > DateTime.Now)… 我不清楚任何地方的cookie,所以我不知道它为什么会过期?

CookieContainer处理路径(谁吃了我的cookie?)

我正在开发一个涉及一些基本网络爬行的项目。 我一直在成功使用HttpWebRequest和HttpWebResponse。 对于cookie处理,我只有一个CookieContainer,我每次都分配给HttpWebRequest.CookieContainer。 每次我自动填充新的cookie,不需要我的额外处理。 直到不久之前,当其中一个曾经工作的网站突然停止工作时,这一切都很好。 我有理由相信这是一个有问题的cookie,但是我没有记录过以前的cookie,所以我不是百分百肯定的。 我用以下代码设法模拟了这个问题: CookieContainer cookieJar = new CookieContainer(); Uri uri1 = new Uri(“http://www.somedomain.com/some/path/page1.html”); CookieCollection cookies1 = new CookieCollection(); cookies1.Add(new Cookie(“NoPathCookie”, “Page1Value”)); cookies1.Add(new Cookie(“CookieWithPath”, “Page1Value”, “/some/path/”)); Uri uri2 = new Uri(“http://www.somedomain.com/some/path/page2.html”); CookieCollection cookies2 = new CookieCollection(); cookies2.Add(new Cookie(“NoPathCookie”, “Page2Value”)); cookies2.Add(new Cookie(“CookieWithPath”, “Page2Value”, “/some/path/”)); Uri uri3 = new Uri(“http://www.somedomain.com/some/path/page3.html”); // Add the cookies […]

ASP Identity 2.0:重新生成身份

我无法让ASP Identity按需刷新存储在cookie中的身份。 在Startup.Auth.cs文件中,cookie设置为重新生成,如下所示: app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, LoginPath = new PathString(“/Account/Login”), Provider = new CookieAuthenticationProvider { OnValidateIdentity = SecurityStampValidator.OnValidateIdentity( validateInterval: TimeSpan.FromMinutes(30), regenerateIdentityCallback: ((manager, user) => manager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie)), getUserIdCallback: ((claimsIdentity) => int.Parse(claimsIdentity.GetUserId()))) } }); 但是我无法弄清楚如何在代码中刷新User.Identity上的内容,即在我需要刷新时强制刷新身份cookie。 我希望能够以编程方式使用重新生成身份回调,这可能吗? 我的问题与此类似: 如何使用Asp.Net Identity 2将用户添加到角色后使.AspNet.ApplicationCookie无效? 但是我想刷新而不是使cookie无效。 编辑 在查看链接的问题后,我尝试了以下(没有完整的error handling): IOwinContext context = Request.GetOwinContext(); QuizSparkSignInManager manager = context.Get(); ClaimsIdentity newIdentity […]

尽管Set-Cookie Header(无重定向),HttpWebResponse.Cookies为空

我在努力弄清楚这里有什么问题。 我正在发送登录信息,我可以在Header中看到具有正确值的Set-Cookie,但Cookies集合没有被填满。 这是HTTPS,登录自动重定向,但我使用AllowAutoRedirect = false禁用它以尝试解决此问题。 在此屏幕截图中,您可以轻松查看调试信息以及应该设置cookie。 我将我的httpWebRequest.Cookie设置为新的CookieCollection。 HttpWebRequest httpRequest; CookieContainer reqCookies = new CookieContainer(); string url = “https://example.com”; string[] email = user.Split(‘@’); email[0] = System.Web.HttpUtility.UrlEncode(email[0]); user = email[0] + “@” + email[1]; pass = System.Web.HttpUtility.UrlEncode(pass); string postData = “email=” + user + “&password=” + pass; byte[] byteData = Encoding.UTF8.GetBytes(postData); httpRequest = (HttpWebRequest)WebRequest.Create(url); httpRequest.Method = “POST”; […]