我想在发布请求后保存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; }}
总结 以上是内存溢出为你收集整理的HttpPost Cookies持久化Android全部内容,希望文章能够帮你解决HttpPost Cookies持久化Android所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)