在Android排球庫(kù)中使用Cookie有人知道如何使用com.android.volley庫(kù)將會(huì)話cookie附加到請(qǐng)求嗎?當(dāng)我登錄網(wǎng)站時(shí),它會(huì)給我一個(gè)會(huì)話cookie。瀏覽器會(huì)將該cookie發(fā)送回任何后續(xù)請(qǐng)求。排球似乎沒(méi)有這樣做,至少不是自動(dòng)的。
3 回答

慕少森
TA貢獻(xiàn)2019條經(jīng)驗(yàn) 獲得超9個(gè)贊
vmirinov是對(duì)的!
以下是我解決問(wèn)題的方法:
請(qǐng)求類(lèi):
public class StringRequest extends com.android.volley.toolbox.StringRequest { private final Map<String, String> _params; /** * @param method * @param url * @param params * A {@link HashMap} to post with the request. Null is allowed * and indicates no parameters will be posted along with request. * @param listener * @param errorListener */ public StringRequest(int method, String url, Map<String, String> params, Listener<String> listener, ErrorListener errorListener) { super(method, url, listener, errorListener); _params = params; } @Override protected Map<String, String> getParams() { return _params; } /* (non-Javadoc) * @see com.android.volley.toolbox.StringRequest#parseNetworkResponse(com.android.volley.NetworkResponse) */ @Override protected Response<String> parseNetworkResponse(NetworkResponse response) { // since we don't know which of the two underlying network vehicles // will Volley use, we have to handle and store session cookies manually MyApp.get().checkSessionCookie(response.headers); return super.parseNetworkResponse(response); } /* (non-Javadoc) * @see com.android.volley.Request#getHeaders() */ @Override public Map<String, String> getHeaders() throws AuthFailureError { Map<String, String> headers = super.getHeaders(); if (headers == null || headers.equals(Collections.emptyMap())) { headers = new HashMap<String, String>(); } MyApp.get().addSessionCookie(headers); return headers; }}
和MyApp:
public class MyApp extends Application { private static final String SET_COOKIE_KEY = "Set-Cookie"; private static final String COOKIE_KEY = "Cookie"; private static final String SESSION_COOKIE = "sessionid"; private static MyApp _instance; private RequestQueue _requestQueue; private SharedPreferences _preferences; public static MyApp get() { return _instance; } @Override public void onCreate() { super.onCreate(); _instance = this; _preferences = PreferenceManager.getDefaultSharedPreferences(this); _requestQueue = Volley.newRequestQueue(this); } public RequestQueue getRequestQueue() { return _requestQueue; } /** * Checks the response headers for session cookie and saves it * if it finds it. * @param headers Response Headers. */ public final void checkSessionCookie(Map<String, String> headers) { if (headers.containsKey(SET_COOKIE_KEY) && headers.get(SET_COOKIE_KEY).startsWith(SESSION_COOKIE)) { String cookie = headers.get(SET_COOKIE_KEY); if (cookie.length() > 0) { String[] splitCookie = cookie.split(";"); String[] splitSessionId = splitCookie[0].split("="); cookie = splitSessionId[1]; Editor prefEditor = _preferences.edit(); prefEditor.putString(SESSION_COOKIE, cookie); prefEditor.commit(); } } } /** * Adds session cookie to headers if exists. * @param headers */ public final void addSessionCookie(Map<String, String> headers) { String sessionId = _preferences.getString(SESSION_COOKIE, ""); if (sessionId.length() > 0) { StringBuilder builder = new StringBuilder(); builder.append(SESSION_COOKIE); builder.append("="); builder.append(sessionId); if (headers.containsKey(COOKIE_KEY)) { builder.append("; "); builder.append(headers.get(COOKIE_KEY)); } headers.put(COOKIE_KEY, builder.toString()); } }}

UYOU
TA貢獻(xiàn)1878條經(jīng)驗(yàn) 獲得超4個(gè)贊
Volley的默認(rèn)HTTP傳輸代碼是HttpUrlConnection
。如果我正確閱讀文檔,您需要選擇自動(dòng)會(huì)話cookie支持:
CookieManager cookieManager = new CookieManager();CookieHandler.setDefault(cookieManager);
另請(qǐng)參見(jiàn)帶有CookieManager的HttpURLConnection是否會(huì)自動(dòng)處理會(huì)話cookie?
- 3 回答
- 0 關(guān)注
- 392 瀏覽
添加回答
舉報(bào)
0/150
提交
取消