set-cookie 怎么获取
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了set-cookie 怎么获取相关的知识,希望对你有一定的参考价值。
参考技术A Cookie的两种获取方式:方法一:
[java] view plain copy
<span style="white-space:pre"> </span>List<Cookie> cookies = ((AbstractHttpClient) mHttpClient).getCookieStore().getCookies();
if(cookies != null)
String tmpcookies = "";
for (Cookie ck : cookies)
// tmpcookies += ck.toString()+";";
tmpcookies += ck.getName()+"="+ck.getValue()+";"+"domain="+ck.getDomain()+";"+"path="+ck.getPath();
方法二:(注意这种方法获取的Cookie不包含Domain)
[java] view plain copy
<span style="white-space:pre"> </span>HttpResponse httpResponse = mHttpClient.execute(httpPost);
response = EntityUtils.toString(httpResponse.getEntity());
if(httpResponse.getFirstHeader("Set-Cookie")!=null)
org.apache.http.Header[] Cookies=httpResponse.getHeaders("Set-Cookie");
StringBuffer stringCookie = new StringBuffer();
for (int j = 0; j < Cookies.length; j++)
stringCookie.append(Cookies[j].getValue()).append(";");
stringCookie.append("domain=192.168.0.184;");
管理Cookie的方法:
[java] view plain copy
<span style="white-space:pre"> </span>CookieSyncManager.createInstance(this);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
// cookieManager.removeSessionCookie();
cookieManager.setCookie(url, cookieString);
CookieSyncManager.getInstance().sync();
清楚所有的Cookie:
[java] view plain copy
CookieSyncManager.createInstance(this);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookie();
CookieSyncManager.getInstance().sync();
php怎么获得头文件中的Set-Cookie
就是怎么获取一个页面的Set-Cookie
参考如下:/*-----保存COOKIE-----*/
$url = \'IP\'; //url地址
$post = "id=user&pwd=123456"; //POST数据
$ch = curl_init($url); //初始化
curl_setopt($ch,CURLOPT_HEADER,1); //将头文件的信息作为数据流输出
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); //返回获取的输出文本流
curl_setopt($ch,CURLOPT_POSTFIELDS,$post); //发送POST数据
$content = curl_exec($ch); //执行curl并赋值给$content
preg_match(\'/Set-Cookie:(.*);/iU\',$content,$str); //正则匹配
$cookie = $str[1]; //获得COOKIE(SESSIONID)
curl_close($ch); //关闭curl
/*-----使用COOKIE-----*/
curl_setopt($ch,CURLOPT_COOKIE,$cookie); 参考技术A 先curl页面然后用正则
preg_match('/set-cookie:(.*)/U');追问
能具体点吗?不太懂
追答<?php$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'website_url');
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$results = curl_exec($ch);
curl_close($ch);
preg_match_all('|Set-Cookie: (.*);|U', $results, $arr);
$cookies = implode(';', $arr[1]);
var_dump($cookies);
?>追问
想把得到的数据 setcookie('xx',xxx); 保存下来,怎么做
追答你的密码泄露了,记得改
本回答被提问者和网友采纳 参考技术B 你打印下全局数组$_SERVER;应该能找到你要寻找的项追问没有哦
参考技术C $_sever 里面有么追问貌似没有
以上是关于set-cookie 怎么获取的主要内容,如果未能解决你的问题,请参考以下文章