在POST调用服务器后获取cookie

时间:2014-05-01 21:59:35

标签: android apache http cookies

我需要通过POST调用将我的客户端验证到服务器,以便能够从服务器下载文件。

这是我的连接类:

package com.pipodi.itasamobile.connections;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;

public class ItaSALogin {

private String username;
private String password;
private DefaultHttpClient httpClient = new DefaultHttpClient();
private HttpContext localContext = new BasicHttpContext();
private HttpPost httpPost = null;

public ItaSALogin(String username, String password) {
    this.username = username;
    this.password = password;
}

public void loginToItaSA() {
    httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY,
            CookiePolicy.RFC_2109);
    httpPost = new HttpPost("http://www.italiansubs.net");
    httpPost.setHeader("User-Agent", "Mozilla/5.0 (X11; U; Linux " +
            "i686; en-US; rv:1.8.1.6) Gecko/20061201 Firefox/2.0.0.6 (Ubuntu-feisty)");
    httpPost.setHeader(
            "Accept",
            "text/html,application/xml,"
                    + "application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
    httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
    List<NameValuePair> pairs = new ArrayList<NameValuePair>();
    pairs.add(new BasicNameValuePair("username", username));
    pairs.add(new BasicNameValuePair("passwd", password));
    pairs.add(new BasicNameValuePair("remember", "yes"));
    pairs.add(new BasicNameValuePair("option", "com_user"));
    pairs.add(new BasicNameValuePair("task", "login"));
    pairs.add(new BasicNameValuePair("silent", "true"));
    try {
        httpPost.setEntity(new UrlEncodedFormEntity(pairs));
        httpClient.execute(httpPost,localContext);
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
}

以下我称之为:

package com.pipodi.itasamobile.connections;

import com.pipodi.itasamobile.MainActivity;
import android.app.DownloadManager;
import android.app.DownloadManager.Request;
import android.content.Context;
import android.net.Uri;
import android.os.AsyncTask;
import android.util.Log;

public class ConnectionForSubtitle extends AsyncTask<String, String, String>{

private int id;

public ConnectionForSubtitle(int id){
    this.id = id;
}

@Override
protected String doInBackground(String... params) {
    ItaSALogin login = new ItaSALogin("username", "password");
    login.loginToItaSA();
    String servicestring = Context.DOWNLOAD_SERVICE;
    DownloadManager downloadmanager;
    downloadmanager = (DownloadManager) MainActivity.context.getSystemService(servicestring);
    Uri uri = Uri
      .parse("https://api.italiansubs.net/api/rest/subtitles/download?subtitle_id=" + this.id + "&authcode=" + "0a7623231022fde8b519d5f6d3084700" + "&apikey=" + Constants.APIKey);
    Log.e("URL", "https://api.italiansubs.net/api/rest/subtitles/download?subtitle_id=" + this.id + "&authcode=" + "0a7623231022fde8b519d5f6d3084700" + "&apikey=" + Constants.APIKey);
    DownloadManager.Request request = new Request(uri);
    Long reference = downloadmanager.enqueue(request);
    return null;
}

}

当我点击我的应用程序中的按钮时,它会调用登录方法然后尝试从服务器下载相应的文件。但是它下载了一个html页面,其中写了下载区域仅供注册用户使用,就像客户端没有登录到服务器一样。我认为这是一个与cookie有关的问题;事实上,如果我从日志中复制URL,则可以从我的PC中正确获取和下载文件。

这里是身份验证的LogCat:

05-01 23:34:51.858: W/ResponseProcessCookies(19725): Invalid cookie header: "Set-Cookie:PHPSESSID=f2c7d7510de65bb976f9a02ec647290d; expires=Wed, 07-May-2014 23:34:49 GMT; Max-Age=525600; path=/; domain=.italiansubs.net". Unable to parse expires attribute: Wed
05-01 23:34:51.868: W/ResponseProcessCookies(19725): Invalid cookie header: "Set-Cookie: SMFCookie17499=a%3A4%3A%7Bi%3A0%3Bs%3A6%3A%22426274%22%3Bi%3A1%3Bs%3A40%3A%22a6194c688245698aeb34330b27219f00b6115155%22%3Bi%3A2%3Bi%3A1430516089%3Bi%3A3%3Bi%3A2%3B%7D; expires=Fri, 01-May-2015 21:34:49 GMT; Max-Age=31536000; path=/; domain=.italiansubs.net". Unable to parse expires attribute: Fri
05-01 23:34:51.873: W/ResponseProcessCookies(19725): Invalid cookie header: "Set-Cookie: PHPSESSID=f2c7d7510de65bb976f9a02ec647290d; expires=Wed, 07-May-2014 23:34:49 GMT; Max-Age=525600; path=/; domain=.italiansubs.net". Unable to parse expires attribute: Wed
05-01 23:34:51.873: W/ResponseProcessCookies(19725): Invalid cookie header: "Set-Cookie: Max-Age=31536000; expires=Fri, 01-May-2015 21:34:49 GMT; Max-Age=31536000; path=/; domain=.italiansubs.net". Unable to parse expires attribute: Fri
05-01 23:34:51.873: W/ResponseProcessCookies(19725): Invalid cookie header: "Set-Cookie: PHPSESSID=24e4197ec0d0d3f7a753daf17982f8a7; expires=Wed, 07-May-2014 23:34:49 GMT; Max-Age=525600; path=/; domain=.italiansubs.net". Unable to parse expires attribute: Wed
05-01 23:34:51.878: W/ResponseProcessCookies(19725): Cookie rejected: "BasicClientCookie[version=0,name=wordpress_ciambellame,domain=.italiansubs.net,path=/wp-content/plugins,expiry=null]". Illegal path attribute "/wp-content/plugins". Path of origin: "/"
05-01 23:34:51.878: W/ResponseProcessCookies(19725): Invalid cookie header: "Set-Cookie: cb7d0ded6b8cd9e20ce31b4d792da3e9=+45F+6+E18125E+A+910444A5411+A565B5316+F4B+257+E4634+D45+A5250135E16+E+C594314534041465643+746+C45+C+2+E1A+955+4515453175E4B; expires=Fri, 01-May-2015 21:34:49 GMT; Max-Age=31536000; path=/". Unable to parse expires attribute: Fri

我该如何解决?谢谢你们。

编辑:我设法将Cookie存储到列表中。现在,我如何将这些cookie设置为GET调用?

0 个答案:

没有答案