在Android中如何在POST请求后获取Cookie?
这是我的类的doInBackground方法,它扩展了AsyncTask以执行POST请求在Android中如何在POST请求后获取Cookie?,android,cookies,android-asynctask,Android,Cookies,Android Asynctask,这是我的类的doInBackground方法,它扩展了AsyncTask以执行POST请求 @Override protected Integer doInBackground(String... arg0) { HttpClient client = new DefaultHttpClient(); Log.d("HTTP", "URL:" + arg0[0]); HttpPost post = new HttpPost(arg0[0]); try {
@Override
protected Integer doInBackground(String... arg0) {
HttpClient client = new DefaultHttpClient();
Log.d("HTTP", "URL:" + arg0[0]);
HttpPost post = new HttpPost(arg0[0]);
try {
// Add your data
post.setEntity(new UrlEncodedFormEntity(login));
// Execute HTTP Post Request
HttpResponse response = client.execute(post);
//to Log the response headers
Header[] headers = response.getAllHeaders();
for (int i=0; i < headers.length; i++) {
Header h = headers[i];
Log.i("Header","Header names: "+h.getName());
Log.i("Header", "Header Value: "+h.getValue());
}
int respCode = response.getStatusLine().getStatusCode();
Log.d("HTTP", "POST:"+arg0[0]+" Risposta Server dopo invio: "+respCode);
return respCode;
} catch (IOException e) {
// Gestire nel codice finale
e.printStackTrace();
}
@覆盖
受保护的整数doInBackground(字符串…arg0){
HttpClient=new DefaultHttpClient();
Log.d(“HTTP”,“URL:+arg0[0]);
HttpPost=新的HttpPost(arg0[0]);
试一试{
//添加您的数据
setEntity(新的UrlEncodedFormEntity(登录));
//执行HTTP Post请求
HttpResponse response=client.execute(post);
//记录响应头的步骤
Header[]headers=response.getAllHeaders();
对于(int i=0;i
这是logcat:
01-18 14:44:26.532:I/标题(591):标题名称:日期
01-18 14:44:26.543:I/标题(591):标题值:Sat,2014年1月18日14:44:21 GMT
01-18 14:44:26.543:I/头(591):头名称:服务器
01-18 14:44:26.543:I/头(591):头值:Apache
01-18 14:44:26.543:I/头(591):头名称:设置Cookie
01-18 14:44:26.543:I/头(591):头值:会话id=29AE962BC7CBB648C4B2E35B2A7B4F6;路径=/;域=.forumcommunity.net;仅HttpOnly
01-18 14:44:26.543:I/标题(591):标题名称:X帧选项
01-18 14:44:26.553:I/头(591):头值:拒绝
01-18 14:44:26.553:I/头(591):头名称:设置Cookie
01-18 14:44:26.553:I/头(591):头值:member_id=8712154;expires=Sun,
2015年1月18日14:44:21格林尼治标准时间;路径=/;域=.forumcommunity.net;HttpOnly
01-18 14:44:26.553:I/头(591):头名称:设置Cookie
01-18 14:44:26.572:I/头(591):头值:pass_hash=83e8e0213aa10058b2d014bc0331598d6076bd23f145d03b6ee8a31b2eb25e96;expires=Sun,2015年1月18日14:44:21 GMT;path=/;domain=.forumcommunity.net;HttpOnly
01-18 14:44:26.572:I/标题(591):标题名称:变化
01-18 14:44:26.572:I/头(591):头值:接受编码
01-18 14:44:26.572:I/标题(591):标题名称:P3P
01-18 14:44:26.582:I/头(591):头值:CP=“NOI ADM DEV COM我们的STP”
01-18 14:44:26.582:I/头(591):头名称:连接
01-18 14:44:26.582:I/收割台(591):收割台值:关闭
01-18 14:44:26.602:I/头(591):头名称:传输编码
01-18 14:44:26.602:I/头(591):头值:分块
01-18 14:44:26.602:I/标题(591):标题名称:内容类型
01-18 14:44:26.602:I/标题(591):标题值:text/html;字符集=windows-1252
如何仅获取设置的Cookie头并在Cookie列表中解析它?查看HTTP请求后访问Cookie的示例: 在
client.execute()
之后使用此代码获取cookie:
List cookies=cookieStore.getCookies();
使用HttpContext代替HttpClientContext和HttpResponse代替CloseableHttpResponse可以做同样的事情吗?是的,它们是相同的类,只是在Android上命名不同。