public void processResponseCookie(HttpURLConnection connection) { // ----------------------------------------------------------------------------------------------------- // 1、获取响应的Set-Cookie值,得到响应的Cookie字符串集合 // ----------------------------------------------------------------------------------------------------- Map<String, List<String>> headerFields = connection.getHeaderFields(); List<String> setCookies = headerFields.get("Set-Cookie"); List<String> responseCookies = new ArrayList<String>(); if (null != setCookies && setCookies.size() > 0) { for (String setCookie : setCookies) { if (setCookie.contains(",")) { String[] arrResponseCookies = setCookie.split(","); for (String rCookie : arrResponseCookies) { responseCookies.add(rCookie); } } else { responseCookies.add(setCookie); } } } else { // 服务端没有响应数据过来,直接不处理了 return; } // ----------------------------------------------------------------------------------------------------- // 2、获取CookieOrigin // ----------------------------------------------------------------------------------------------------- CookieOrigin cookieOrigin = getCookieOrigin(connection); // ----------------------------------------------------------------------------------------------------- // 3、根据响应的Cookie字符串集合与CookieOrigin,获取Cookie集合 // ----------------------------------------------------------------------------------------------------- CookieSpec cookieSpec = obtainCookieSpec(); try { List<Cookie> cookies = cookieSpec.parse(responseCookies, cookieOrigin); // ----------------------------------------------------------------------------------------------------- // 4、将验证服务端返回的Cookie,并存起来 // ----------------------------------------------------------------------------------------------------- for (Cookie cookie : cookies) { try { cookieSpec.validate(cookie, cookieOrigin); // 验证Cookie,不符合规则的将抛MalformedCookieException CookieStoreHandler.getInstance().addCookie(cookie); } catch (MalformedCookieException ex) { L.w("Cookie rejected [" + formatCooke(cookie) + "] " + ex.getMessage()); } } } catch (MalformedCookieException ex) { L.w("Invalid Cookie Header [" + responseCookies + "] " + ex.getMessage()); } }
// post请求,参数有文件流(文件上传)与普通字符串参数。响应是json,以指定类的实例对象返回请求结果 private void postJsonFile() { // 简化,实际要判断Environment.getExternalStorageState()状态,及sdcard中是否存在111.png与222.png File file1 = new File(Environment.getExternalStorageDirectory(), "111.jpg"); File file2 = new File(Environment.getExternalStorageDirectory(), "222.jpg"); if (file1.exists()) { L.e("file1存在,大小:" + file1.length()); } else { L.e("file1不存在,大小:"); } if (file2.exists()) { L.e("file2存在,大小:" + file2.length()); } else { L.e("file2不存在,大小:"); } String url = "http://192.168.1.102:8080/Test/UploadHandleServlet"; RequestInfo requestInfo = new RequestInfo(); requestInfo.url = url; requestInfo.setParams("eamil", "*****@*****.**"); requestInfo.setParams("mobileNum", "18688888888"); // 普通字符串参数 requestInfo.setParams("file1", file1); // 上传的文件1 requestInfo.setParams("file2", file2); // 上传的文件2 requestInfo.isShowLoadingDialog = false; afeitaNet.post( requestInfo, new NetCallback<String>() { @Override public void onResult(String response) { Toast.makeText( NetSampleActivity.this, "get HelloServlet post 调用成功:" + response, Toast.LENGTH_LONG) .show(); } @Override public void onFinish(boolean isCancel) { if (null != dialog) { dialog.dismiss(); dialog = null; } } // -----可选监听文件上传进度,并显示进度 @Override public void onUpload( int fileNum, String currentUploadingFilename, long sumSize, long sumDonedSize, long sumSpendedTime) { if (null == dialog) { dialog = new UploadFileProgreeDialog(NetSampleActivity.this); dialog.show(); } dialog.setFileTip(currentUploadingFilename); int precent = (int) ((sumDonedSize * 1.0 / sumSize) * 100); String strSumSize = Formatter.formatFileSize(NetSampleActivity.this, sumSize); String strSumDonedSize = Formatter.formatFileSize(NetSampleActivity.this, sumDonedSize); String strProgress = strSumDonedSize + "/" + strSumSize; dialog.setProgress(precent, strProgress, precent + "%"); } }); }