ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

HttpPost Cookies持久化Android

2019-11-01 01:23:50  阅读:192  来源: 互联网

标签:httpclient http-post cookiestore android


我想在发布请求后保存Cookie. httpClient实现的类:

 public class JSONParser {
    CookieStore store = new BasicCookieStore();
       static InputStream is = null;
        static JSONObject jObj = null;
        static String json = "";
        private static final DefaultHttpClient httpClient = new DefaultHttpClient() ;
     public static DefaultHttpClient getInstance() { return httpClient; }

    public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) {

        // Making HTTP request
        try {
            CookieStore cookieStore = new BasicCookieStore();

            // Create local HTTP context
            HttpContext localContext = new BasicHttpContext();

            // Bind custom cookie store to the local context
            localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

            DefaultHttpClient httpClient = JSONParser.getInstance();
            List<Cookie> cookies = httpClient.getCookieStore().getCookies();
            httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "Android-AEApp,ID=2435743");
            httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2109);
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new UrlEncodedFormEntity(params));
            httpPost.setHeader("User-Agent","Android-AEApp,ID=2435743");

            cookies = httpClient.getCookieStore().getCookies();
            httpPost.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
            httpPost.setHeader("Cookie", "PHPSESSID=lc89a2uu0rj6t2p219gc2cq4i2; path=/");
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            cookies = httpClient.getCookieStore().getCookies();

            if (cookies.isEmpty()) {
                for (Cookie a : cookies)
                cookieStore.addCookie(a);
            } else {
                for (Cookie a : cookies) {
                    cookieStore.addCookie(a);
                    System.out.println("- " + a.getValue().toString());
                }
            }

            is = httpEntity.getContent();


        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
       }

我为DefaultHttpClient创建singlotone,并希望将其用于其他请求.当我尝试使像这样:

 HttpResponse httpResponse = httpClient.execute(httpPost, localContext);

我没有饼干.当我不包括localContext时,我可以使用

 if (cookies.isEmpty()) {
                for (Cookie a : cookies)
                cookieStore.addCookie(a);
            } else {
                for (Cookie a : cookies) {
                    cookieStore.addCookie(a);
                    System.out.println("- " + a.getValue().toString());
                }
            }

但是cookie不会在第二个请求时设置.我猜-原因是不使用localContext.告诉我有什么问题,如果更改了cookie,如何设置cookie?

解决方法:

我对Cookie的ArrayList使用了Singletone.看起来像这样:

public class CookieStorage {


    private ArrayList<Object> arrayList;

    private static CookieStorage instance;

    private CookieStorage(){
        arrayList = new ArrayList<Object>();
    }

    public static CookieStorage getInstance(){
        if (instance == null){
            instance = new CookieStorage();
        }
        return instance;
    }

    public ArrayList<Object> getArrayList() {
        return arrayList;
    }

    @Override
    public String toString()
    {       
    return getArrayList().toString();   
    }
}

和我的HttpClient类:

public class HttpClient {
static InputStream is = null;
private static final DefaultHttpClient httpClient = new DefaultHttpClient() ;
public static DefaultHttpClient getInstance() { return httpClient; }

 public static InputStream getResponse(String url, List<NameValuePair> params)
 {
    try {
    CookieStore cookieStore = new BasicCookieStore();
    DefaultHttpClient httpClient = JSONParser.getInstance();        
    httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "Android-AEApp,ID=2435743");
    httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2109);
    BasicClientCookie cookie = new BasicClientCookie("PHPSESSID", "1");  
    httpClient.setCookieStore(cookieStore);
    HttpPost httpPost = new HttpPost(url);
    httpPost.setEntity(new UrlEncodedFormEntity(params));
    httpPost.setHeader("User-Agent","Android-AEApp,ID=2435743");
    httpPost.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
    System.out.println(CookieStorage.getInstance().toString());
    if (CookieStorage.getInstance().getArrayList().isEmpty())
        CookieStorage.getInstance().getArrayList().add("PHPSESSID=lc89a2uu0rj6t2p219gc2cq4i2");
    httpPost.setHeader("Cookie", CookieStorage.getInstance().getArrayList().get(0).toString());
    HttpResponse httpResponse = httpClient.execute(httpPost);
    Header[] head = httpResponse.getAllHeaders();
    System.out.println(cookie);

    if (httpResponse.getLastHeader("Set-Cookie")!=null)
    {

        CookieStorage.getInstance().getArrayList().remove(0);
        CookieStorage.getInstance().getArrayList().add(httpResponse.getLastHeader("Set-Cookie").getValue());
    }
    Log.i("arrayList",(CookieStorage.getInstance().getArrayList().toString()));

    HttpEntity httpEntity = httpResponse.getEntity();
    is = httpEntity.getContent();


    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

     return is;
 }


}

标签:httpclient,http-post,cookiestore,android
来源: https://codeday.me/bug/20191101/1980118.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有