【发布时间】:2011-12-14 12:24:19
【问题描述】:
我想获取网站的会话 cookie。不幸的是,“Set-Cookie”-Header 没有出现。
这是我编写的代码: “commands”是一个String[][],整个代码被try/catch包裹起来。
CookieStore cookieStore = new BasicCookieStore();
HttpContext localContext = new BasicHttpContext();
localContext.setAttribute(ClientContext.COOKIE_STORE,cookieStore);
HttpPost httppost = new HttpPost(url);
List<NameValuePair> nvps = new ArrayList<NameValuePair>(0);
for (int i=0;i<commands.length;++i)
nvps.add(new BasicNameValuePair(commands[i][0],commands[i][1]));
httppost.setEntity(new UrlEncodedFormEntity(nvps,HTTP.UTF_8));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
Header[] headers = response.getAllHeaders();
List<Cookie> cookies = cookieStore.getCookies();
String data = EntityUtils.toString(entity);
我对Http通信的理解告诉我应该有一个“Set-Cookie”的Header。我从 response.getAllHeaders() 获得的唯一标头是 Connection:close、X-Powered-By:PHP/4.3.4 和 Content-Type:text/html
返回的数据中包含一些 javascript (response.getEntity())。
<script language = "javascript">
<!--
location.href="/index.php";
function SetCookie(name,value,expire,path){
document.cookie = name + "=" + escape(value) + ((path == null) ? "":(";path="+path))
}
var iad = 461180104
SetCookie("iad",iad,0,"/")
-->
</script>
据我了解,这段代码永远不会执行,因为它只是一个注释?! 但这也可能是应该创建 cookie 的地方。
有什么想法吗?
更新: “Opera Mobile”是我发现的唯一一款对本网站的 cookie 没有问题的 Android 浏览器。 “Opera Mini”、“Dolphin HD”和 Froyo Stock 浏览器都失败了。没有桌面浏览器有连接问题。这是一个 webkit 问题吗?如果是这种情况:如何避免?
【问题讨论】:
-
仅供参考,也许有帮助:“var iad =”后面的数字每次访问都会改变。