我如何挑选特定的Cookie? [英] How do i pick specific cookies?

查看:133
本文介绍了我如何挑选特定的Cookie?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从响应中选择特定的Cookie?

How can I select specific cookies from a response?

我得到的响应有6个Set-Cookie行,但是下一篇文章只需要其中一些.

The response I'm getting has 6 Set-Cookie rows, but I just need some of them for my next post.

HTTP/1.1 200 OK
date: Thu, 05 Mar 2015 13:49:29 GMT
cache-control: no-cache="set-cookie, set-cookie2"
expires: Thu, 01 Dec 1994 16:00:00 GMT
Set-Cookie: JSESSIONID-AUTH=0000Q77IB2vtdMjRmqnsja8ciUE:18j7lq1fl;Secure; Path=/
Set-Cookie: PD_STATEFUL_e0255922-d1d6-11e3-9144-005056bc2960=%2Fnauth2;Secure; Path=/
Set-Cookie: PD-SESSION-ID=1_4_0_Mip9xQRE1J80beniD1eh-7Le1L+X8uwfIRVUZdKvJUKO2OIB;Secure; Path=/; HttpOnly
Set-Cookie: iampsc1110=rd520o00000000000000000000ffff0a101fa3o1110;secure; path=/
Set-Cookie: TSaee27a=e4d514b3ab1503842b07e9b4d4ee0db30a6a3a54c730b09754f85edbe54ca44a641b9f7bf3fdf509ca7d6de2ed2d4e69c8c3db3f6623dd16fb85456b4ced6f5a34c171e7a460affd34c171e70025563134c171e75f534b1f34c171e7; Path=/

推荐答案

您是对的,目前尚无合适的方法.以下是两种解决方法:

You're right, there is no proper way to do this yet. Here are 2 workarounds:

手动,您可以删除不需要的Cookie.在请求菜单中,选择 Cookies ,然后选择 Show Cookies .使用搜索框查找您要删除的Cookie.

Manually, you can go and delete the cookies you don't want. In the Requests menu, pick Cookies then Show Cookies. Use the search box to find the cookies you'd like to get rid of.

更复杂的解决方案,但我认为这完全符合您的需求.而是使用 Custom 动态值(右键单击该字段,然后选择扩展名> Custom ),然后使用以下JavaScript代码段:

A more complex solution, but that exactly fits your need, I think. Use a Custom dynamic value (right click on the field, and pick Extensions > Custom), instead, and use the following JavaScript code snippet:

function evaluate(context){
  // Set here the cookies you'd like to return
  var wantedCookies = ["datr", "reg_fb_ref"];

  var regex = /^(\w+)\=([^;\s]+)/g;

  // Request
  // Uses here the current request, you can use getRequestByName("name of the request") instead
  var request = context.getCurrentRequest();

  // Get response cookies
  var cookies = request.getLastExchange().getResponseHeaderByName("Set-Cookie").split(", ");
  var filteredCookies = [];

  for (var i in cookies) {
    var cookie = cookies[i];
    var match = regex.exec(cookie);
    if (match && wantedCookies.indexOf(match[1]) >= 0) {
      filteredCookies.push(match[0]);
    }
  }

  return filteredCookies.join(",");
};

这基本上是手动解析响应cookie,并返回您所需的cookie.

That basically parses manually the response cookies, and returns the ones you need.

这篇关于我如何挑选特定的Cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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