Android Http 获取会话 Cookie

编程入门 行业动态 更新时间:2024-10-18 12:26:00
本文介绍了Android Http 获取会话 Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我真的不想在这里发帖,因为网络上的信息太多了,但我已经摸索了很多深度,无法弄清楚.

I didn't really want to post here as there is so much information on the net, but I've trawled the depths and can't figure it out.

好的,所以我无法在两种情况下使用它,希望两者的答案都相同.

Ok so I can't get this to work in two scenario's hopefully the answer is the same for both.

我的问题是我设置了请求标头,但它似乎没有发送.

My problem is I set the request header but it doesn't seem to send it.

我有一个会话 ID s=e32ff223fwefd3 ,我想将它存储在 "Cookie" 下,但它似乎不起作用.

I have a session id s=e32ff223fwefd3 , and I want to store this under "Cookie" , but it doesn't seem to be working.

这是我拥有的最快的代码示例

Here is the quickest code example I have

import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.HttpResponse; private static String sessionCookie = "s=12342342352354234"; public static void get(String url) { HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet(url); if(sessionCookie != null){ Log.d(TAG, "Setting Cookie: "+sessionCookie); request.setHeader("Cookie", sessionCookie); } else { Log.i(TAG, "Null session request get()"); } HttpResponse response = client.execute(request); Header[] headers = response.getAllHeaders(); for (int i=0; i < headers.length; i++) { Header h = headers[i]; Log.i(TAG, "Header names: "+h.getName()); Log.i(TAG, "Header Value: "+h.getValue()); } }

所以当我的回复从另一边出来时,它没有附加我的 s=232342w3f23f id!

So when my response comes out the other side it doesn't have my s=232342w3f23f id attached to it!

我想我已经解释了这一点,感谢任何帮助

I think I've explained this right, any help is appreciated

推荐答案

好的,上面的代码确实有效,只是我没有正确检索标题以查看所有字段.问题是我的会话 ID 无效,但我使用的网络服务器具有最糟糕的错误处理机制 :-) 感谢收听!

Ok the code above actually does work, I just didn't retrieve the header correctly to see all the fields. The issue was I had an invalid session Id but the webserver I was using has the WORST error handling mechanisms :-) thanks for listening!

更多推荐

Android Http 获取会话 Cookie

本文发布于:2023-11-28 14:17:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1642754.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Android   Http   Cookie

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!