java - android使用websocket無法訪問cookie。
問題描述
我的java服務(wù)端為了分清各個用戶使用session里的用戶名做了區(qū)分,html頁面使用沒有問題,但是在android端訪問的時候取不到cookie值。
public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception {if (request instanceof ServletServerHttpRequest) { ServletServerHttpRequest servletRequest = (ServletServerHttpRequest) request; // 鏈接webSocket時傳遞的參數(shù) 標(biāo)識是否是特定分組, Null表明不是 //String type = (String) servletRequest.getServletRequest().getParameter('type'); HttpSession session = servletRequest.getServletRequest().getSession(false); if (session != null) {LoginUserInfo user = (LoginUserInfo) session.getAttribute('loginUserInfo');//System.out.println('id');//System.out.println(session.getId());if (user != null) { attributes.put('user', user); //attributes.put('type', type);Enumeration<String> params = servletRequest.getServletRequest().getParameterNames(); while (params.hasMoreElements()) {String param = params.nextElement();attributes.put(param, servletRequest.getServletRequest().getParameter(param)); }} }}return true; }
//websocket建立
WebSocketClient websocket;/** * 與服務(wù)器建立websocket連接 */private void websocket() { new Thread(new Runnable() {@Overridepublic void run() { //獲取之前的登錄信息 String Cookie = cookie.get(0).getName()+'='+cookie.get(0).getValue()+';'+ cookie.get(4).getName()+'='+cookie.get(4).getValue()+';'+ cookie.get(5).getName()+'='+cookie.get(5).getValue()+';'; application.setCookie(Cookie); Log.i('', Cookie); try {HashMap<String, String> hashMap = new HashMap<String, String>();hashMap.put('Cookie', Cookie);// 這里的url不能帶有http://// 直接使用 xx.xx.xx.xx:端口websocket = new WebSocketClient(new URI(HttpUrlS.WEBSOCKET), new Draft_17(), hashMap, 5000) { @Override public void onOpen(ServerHandshake handshakedata) {Log.d('MyTAG', 'WebSocketClient onOpen'); } @Override public void onMessage(final String message) { //WebSocketUtil.handleMessage(message);Log.i('', '有人說:' + message);runOnUiThread(new Runnable() { @Override public void run() { //Toast.makeText(MainActivity.this, '有人說:' + message, Toast.LENGTH_SHORT).show(); }}); } @Override public void onClose(int code, String reason, boolean remote) {Log.d('MyTAG', 'close = ' + reason); } @Override public void onError(Exception ex) {ex.printStackTrace();Log.d('MyTAG', 'onError = ' + ex.toString()); }};websocket.connect(); } catch (URISyntaxException e) {e.printStackTrace(); }} }).start();}
問題解答
回答1:他沒傳吧,自定義的loginUserInfo需要android手動傳
回答2:你好,請問一下Android怎么自定義的loginUserInfo手動傳?
相關(guān)文章:
1. 查詢mysql數(shù)據(jù)庫中指定表指定日期的數(shù)據(jù)?有詳細(xì)2. mysql - 怎么生成這個sql表?3. mysql儲存json錯誤4. php - 公眾號文章底部的小程序二維碼如何統(tǒng)計?5. mysql - 表名稱前綴到底有啥用?6. mysql - 數(shù)據(jù)庫表中,兩個表互為外鍵參考如何解決7. Navicat for mysql 中以json格式儲存的數(shù)據(jù)存在大量反斜杠,如何去除?8. 在mybatis使用mysql的ON DUPLICATE KEY UPDATE語法實現(xiàn)存在即更新應(yīng)該使用哪個標(biāo)簽?9. mysql - 數(shù)據(jù)庫建字段,默認(rèn)值空和empty string有什么區(qū)別 11010. sql語句 - 如何在mysql中批量添加用戶?
