xutils 2.x(2.6)中的session獲得和cookieStore使用
package com.lidroid.xutils; import android.text.TextUtils; import com.lidroid.xutils.exception.HttpException; import com.lidroid.xutils.http.*; import com.lidroid.xutils.http.callback.HttpRedirectHandler; import com.lidroid.xutils.http.callback.RequestCallBack; import com.lidroid.xutils.http.client.DefaultSSLSocketFactory; import com.lidroid.xutils.http.client.HttpRequest; import com.lidroid.xutils.http.client.RetryHandler; import com.lidroid.xutils.http.client.entity.GZipDecompressingEntity; import com.lidroid.xutils.task.PriorityExecutor; import com.lidroid.xutils.util.OtherUtils; import org.apache.http.*; import org.apache.http.client.CookieStore; import org.apache.http.client.HttpClient; import org.apache.http.client.protocol.ClientContext; import org.apache.http.conn.params.ConnManagerParams; import org.apache.http.conn.params.ConnPerRouteBean; import org.apache.http.conn.scheme.PlainSocketFactory; import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.scheme.SchemeRegistry; import org.apache.http.conn.ssl.SSLSocketFactory; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager; import org.apache.http.params.BasicHttpParams; import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpParams; import org.apache.http.params.HttpProtocolParams; import org.apache.http.protocol.BasicHttpContext; import org.apache.http.protocol.HTTP; import org.apache.http.protocol.HttpContext; import java.io.File; import java.io.IOException; public class HttpUtils { public final static HttpCache sHttpCache = new HttpCache(); private final DefaultHttpClient httpClient; private final HttpContext httpContext = new BasicHttpContext(); private HttpRedirectHandler httpRedirectHandler; public HttpUtils() { this(HttpUtils.DEFAULT_CONN_TIMEOUT, null); } public HttpUtils(int connTimeout) { this(connTimeout, null); } public HttpUtils(String userAgent) { this(HttpUtils.DEFAULT_CONN_TIMEOUT, userAgent); } public HttpUtils(int connTimeout, String userAgent) { HttpParams params = new BasicHttpParams(); ConnManagerParams.setTimeout(params, connTimeout); HttpConnectionParams.setSoTimeout(params, connTimeout); HttpConnectionParams.setConnectionTimeout(params, connTimeout); if (TextUtils.isEmpty(userAgent)) { userAgent = OtherUtils.getUserAgent(null); } HttpProtocolParams.setUserAgent(params, userAgent); ConnManagerParams.setMaxConnectionsPerRoute(params, new ConnPerRouteBean(10)); ConnManagerParams.setMaxTotalConnections(params, 10); HttpConnectionParams.setTcpNoDelay(params, true); HttpConnectionParams.setSocketBufferSize(params, 1024 * 8); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); schemeRegistry.register(new Scheme("https", DefaultSSLSocketFactory.getSocketFactory(), 443)); httpClient = new DefaultHttpClient(new ThreadSafeClientConnManager(params, schemeRegistry), params); httpClient.setHttpRequestRetryHandler(new RetryHandler(DEFAULT_RETRY_TIMES)); httpClient.addRequestInterceptor(new HttpRequestInterceptor() { @Override public void process(org.apache.http.HttpRequest httpRequest, HttpContext httpContext) throws org.apache.http.HttpException, IOException { if (!httpRequest.containsHeader(HEADER_ACCEPT_ENCODING)) { httpRequest.addHeader(HEADER_ACCEPT_ENCODING, ENCODING_GZIP); } } }); httpClient.addResponseInterceptor(new HttpResponseInterceptor() { @Override public void process(HttpResponse response, HttpContext httpContext) throws org.apache.http.HttpException, IOException { final HttpEntity entity = response.getEntity(); if (entity == null) { return; } final Header encoding = entity.getContentEncoding(); if (encoding != null) { for (HeaderElement element : encoding.getElements()) { if (element.getName().equalsIgnoreCase("gzip")) { response.setEntity(new GZipDecompressingEntity(response.getEntity())); return; } } } } }); } // ************************************ default settings & fields **************************** private String responseTextCharset = HTTP.UTF_8; private long currentRequestExpiry = HttpCache.getDefaultExpiryTime(); private final static int DEFAULT_CONN_TIMEOUT = 1000 * 15; // 15s private final static int DEFAULT_RETRY_TIMES = 3; private static final String HEADER_ACCEPT_ENCODING = "Accept-Encoding"; private static final String ENCODING_GZIP = "gzip"; private final static int DEFAULT_POOL_SIZE = 3; private final static PriorityExecutor EXECUTOR = new PriorityExecutor(DEFAULT_POOL_SIZE); public HttpClient getHttpClient() { return this.httpClient; } // ***************************************** config ******************************************* public HttpUtils configResponseTextCharset(String charSet) { if (!TextUtils.isEmpty(charSet)) { this.responseTextCharset = charSet; } return this; } public HttpUtils configHttpRedirectHandler(HttpRedirectHandler httpRedirectHandler) { this.httpRedirectHandler = httpRedirectHandler; return this; } public HttpUtils configHttpCacheSize(int httpCacheSize) { sHttpCache.setCacheSize(httpCacheSize); return this; } public HttpUtils configDefaultHttpCacheExpiry(long defaultExpiry) { HttpCache.setDefaultExpiryTime(defaultExpiry); currentRequestExpiry = HttpCache.getDefaultExpiryTime(); return this; } public HttpUtils configCurrentHttpCacheExpiry(long currRequestExpiry) { this.currentRequestExpiry = currRequestExpiry; return this; } public HttpUtils configCookieStore(CookieStore cookieStore) { httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); return this; } public HttpUtils configUserAgent(String userAgent) { HttpProtocolParams.setUserAgent(this.httpClient.getParams(), userAgent); return this; } public HttpUtils configTimeout(int timeout) { final HttpParams httpParams = this.httpClient.getParams(); ConnManagerParams.setTimeout(httpParams, timeout); HttpConnectionParams.setConnectionTimeout(httpParams, timeout); return this; } public HttpUtils configSoTimeout(int timeout) { final HttpParams httpParams = this.httpClient.getParams(); HttpConnectionParams.setSoTimeout(httpParams, timeout); return this; } public HttpUtils configRegisterScheme(Scheme scheme) { this.httpClient.getConnectionManager().getSchemeRegistry().register(scheme); return this; } public HttpUtils configSSLSocketFactory(SSLSocketFactory sslSocketFactory) { Scheme scheme = new Scheme("https", sslSocketFactory, 443); this.httpClient.getConnectionManager().getSchemeRegistry().register(scheme); return this; } public HttpUtils configRequestRetryCount(int count) { this.httpClient.setHttpRequestRetryHandler(new RetryHandler(count)); return this; } public HttpUtils configRequestThreadPoolSize(int threadPoolSize) { HttpUtils.EXECUTOR.setPoolSize(threadPoolSize); return this; } // ***************************************** send request ******************************************* public <T> HttpHandler<T> send(HttpRequest.HttpMethod method, String url, RequestCallBack<T> callBack) { return send(method, url, null, callBack); } public <T> HttpHandler<T> send(HttpRequest.HttpMethod method, String url, RequestParams params, RequestCallBack<T> callBack) { if (url == null) throw new IllegalArgumentException("url may not be null"); HttpRequest request = new HttpRequest(method, url); return sendRequest(request, params, callBack); } public ResponseStream sendSync(HttpRequest.HttpMethod method, String url) throws HttpException { return sendSync(method, url, null); } public ResponseStream sendSync(HttpRequest.HttpMethod method, String url, RequestParams params) throws HttpException { if (url == null) throw new IllegalArgumentException("url may not be null"); HttpRequest request = new HttpRequest(method, url); return sendSyncRequest(request, params); } // ***************************************** download ******************************************* public HttpHandler<File> download(String url, String target, RequestCallBack<File> callback) { return download(HttpRequest.HttpMethod.GET, url, target, null, false, false, callback); } public HttpHandler<File> download(String url, String target, boolean autoResume, RequestCallBack<File> callback) { return download(HttpRequest.HttpMethod.GET, url, target, null, autoResume, false, callback); } public HttpHandler<File> download(String url, String target, boolean autoResume, boolean autoRename, RequestCallBack<File> callback) { return download(HttpRequest.HttpMethod.GET, url, target, null, autoResume, autoRename, callback); } public HttpHandler<File> download(String url, String target, RequestParams params, RequestCallBack<File> callback) { return download(HttpRequest.HttpMethod.GET, url, target, params, false, false, callback); } public HttpHandler<File> download(String url, String target, RequestParams params, boolean autoResume, RequestCallBack<File> callback) { return download(HttpRequest.HttpMethod.GET, url, target, params, autoResume, false, callback); } public HttpHandler<File> download(String url, String target, RequestParams params, boolean autoResume, boolean autoRename, RequestCallBack<File> callback) { return download(HttpRequest.HttpMethod.GET, url, target, params, autoResume, autoRename, callback); } public HttpHandler<File> download(HttpRequest.HttpMethod method, String url, String target, RequestParams params, RequestCallBack<File> callback) { return download(method, url, target, params, false, false, callback); } public HttpHandler<File> download(HttpRequest.HttpMethod method, String url, String target, RequestParams params, boolean autoResume, RequestCallBack<File> callback) { return download(method, url, target, params, autoResume, false, callback); } public HttpHandler<File> download(HttpRequest.HttpMethod method, String url, String target, RequestParams params, boolean autoResume, boolean autoRename, RequestCallBack<File> callback) { if (url == null) throw new IllegalArgumentException("url may not be null"); if (target == null) throw new IllegalArgumentException("target may not be null"); HttpRequest request = new HttpRequest(method, url); HttpHandler<File> handler = new HttpHandler<File>(httpClient, httpContext, responseTextCharset, callback); handler.setExpiry(currentRequestExpiry); handler.setHttpRedirectHandler(httpRedirectHandler); if (params != null) { request.setRequestParams(params, handler); handler.setPriority(params.getPriority()); } handler.executeOnExecutor(EXECUTOR, request, target, autoResume, autoRename); return handler; } //////////////////////////////////////////////////////////////////////////////////////////////// private <T> HttpHandler<T> sendRequest(HttpRequest request, RequestParams params, RequestCallBack<T> callBack) { HttpHandler<T> handler = new HttpHandler<T>(httpClient, httpContext, responseTextCharset, callBack); handler.setExpiry(currentRequestExpiry); handler.setHttpRedirectHandler(httpRedirectHandler); request.setRequestParams(params, handler); if (params != null) { handler.setPriority(params.getPriority()); } handler.executeOnExecutor(EXECUTOR, request); return handler; } private ResponseStream sendSyncRequest(HttpRequest request, RequestParams params) throws HttpException { SyncHttpHandler handler = new SyncHttpHandler(httpClient, httpContext, responseTextCharset); handler.setExpiry(currentRequestExpiry); handler.setHttpRedirectHandler(httpRedirectHandler); request.setRequestParams(params); return handler.sendRequest(request); } }
相關推薦
xutils 2.x(2.6)中的session獲得和cookieStore使用
package com.lidroid.xutils; import android.text.TextUtils; import com.lidroid.xutils.exception.HttpException; import com.lidroid.xutils.http.*; import com
Spring Boot 2.x(十六):玩轉vue檔案上傳
為什麼使用Vue-Simple-Uploader 最近用到了Vue + Spring Boot來完成檔案上傳的操作,踩了一些坑,對比了一些Vue的元件,發現了一個很好用的元件——Vue-Simple-Uploader,先附上gayhub的 ,再說說為什麼選用這個元件,對比vue-ant-design和elem
Spring Boot 2.x(十七):快速入門Elastic Search
What —— Elasticsearch是什麼? Elasticsearch是一個基於Lucene的搜尋伺服器,Elasticsearch也是使用Java編寫的,它的內部使用Lucene做索引與搜尋,但是它的目的是使用全文檢索變得簡單,通過隱藏Lucene的複雜性,取而代之的提供一套簡單一致的RESTful
Spring Boot 2.x (十八):郵件服務一文打盡
前景介紹 在日常的工作中,我們經常會用到郵件服務,比如傳送驗證碼,找回密碼確認,註冊時郵件驗證等,所以今天在這裡進行郵件服務的一些操作。 大致思路 我們要做的其實就是把Java程式作為一個客戶端,然後通過配置SMTP協議去連線我們所使用的傳送郵箱(from)對應的SMTP伺服器,然後通過SMTP協議,將郵件轉
Spring Boot 2.x(十一):AOP實戰--列印介面日誌
介面日誌有啥用 在我們日常的開發過程中,我們可以通過介面日誌去檢視這個介面的一些詳細資訊。比如客戶端的IP,客戶端的型別,響應
SpringBoot 2.x(三):整合Mybatis的四種方式
前言 目前的大環境下,使用Mybatis作為持久層框架還是佔了絕大多數的,下面我們來說一下使用Mybatis的幾種姿勢。 姿勢一:零配置註解開發 第一步:引入依賴 首先,我們需要在pom檔案中新增依賴: 第二步:配置檔案 這裡我們採用yml來進行編寫,與properties檔案相比,yml看
SpringBoot 2.x(五):整合Mybatis-Plus
簡介 Mybatis-Plus是在Mybatis的基礎上,國人開發的一款持久層框架。 並且榮獲了2018年度開源中國最受歡迎的中國軟體TOP5 同樣以簡化開發為宗旨的Spring Boot與Mybatis-Plus放在一起會產生什麼樣的化學反應呢?下面我們來領略一下兩者配合帶來的效率上的提升。 Myba
Spring Boot 2.x(六):優雅的統一返回值
為什麼要統一返回值 在我們做後端應用的時候,前後端分離的情況下,我們經常會定義一個數據格式,通常會包含code,message,data這三個必不可少的資訊來方便我們的交流,下面我們直接來看程式碼 ReturnVO package indi.viyoung.viboot.util; import ja
sql優化實戰:從6秒+到2秒(使用索引)
今天客服反饋 客戶在前兩天查詢一個移動端報表時報錯了。 我看了一下報錯資訊,大致是timeout,這種錯誤基本可以確定是由於查詢時間超過閥值(一般為3秒)。 sql程式碼如下: SELECT hh.EMP_ID , hh.STORE_ID ,
Log4j 1.x 升級 Log4j 2.x (調研和升級)
因為公司業務需要,目前的log4j 1.x 遇到死鎖,需要升級到Log4j 2.x。現在對目前的日誌框架進行調研,並根據目前的現狀提出升級的方法。 一引言 對於一個應用程式來說日誌記錄是必不可少的一部分。線上問題追蹤,基於日誌的業務邏輯統計分析等都離不日誌
Spring Boot 2.x (十):構建優雅的RESTful接口
github 統一 spring 發送 註意 water quest 優雅 ring RESTful 相信在座的各位對於RESTful都是略有耳聞,那麽RESTful到底是什麽呢? REST(Representational State Transfer)表述性狀態轉移是
圖的遍歷之DSF深度優先演算法6.2.1(網路整理)
圖的遍歷之深度優先演算法虛擬碼描述(和樹的前序遍歷相似,實際上樹可以看成特殊的圖:N個頂點有N-1條邊,不曾在迴路!即樹是圖連通中最少邊的情況) 圖片來自網路 如上圖: 深度優先遍歷: 先選取一個頂點訪問它,然後深度優先遍歷它的每個未訪問的鄰接點 #include&l
(C語言列印圖形題2)程式設計,輸入n,輸出如下例(n=6)所示的圖形:
程式設計,輸入n,輸出如下例(n=6)所示的圖形: * * * * * * * * * * * *
init 0-6 (啟動級別:init 0,1,2,3,4,5,6)
啟動級別: init 0,1,2,3,4,5,6 這是個很久的知識點了,只是自己一直都迷迷糊糊的,今天在翻出來好好理解下。。 0
Spring Boot 2.x(四):整合Mybatis的四種方式
前言 目前的大環境下,使用Mybatis作為持久層框架還是佔了絕大多數的,下面我們來說一下使用Mybatis的幾種姿勢。 姿勢一:
Spring Boot 2.x(五):整合Mybatis-Plus
簡介 Mybatis-Plus是在Mybatis的基礎上,國人開發的一款持久層框架。 並且榮獲了2018年度開源中國最受歡迎的中
百度地圖3.2教程(2)公交查詢
track error alt 3.1 內容 說了 ase 放大 gets 上一篇地址 百度地圖3.1教程—檢索功能演示 還記得兩天前 我在找公交接口,非常不幸。接口有些查不到,幾年的前的數據,哎 。算了唄,突然想起來了百度地圖有這個功能,結果去看了API,尼瑪這
初識vue 2.0(2):路由與組件
組件化 script -128 watch css image 暫時 效果 默認 1,在上一篇的創建工程中,使用的的模版 webpack-simple 只是創建了一個簡單的demo,並沒有組件和路由功能,此次采用了webpack模版,自動生成組件和路由。^_^ 在模版初始
thinkphp3.2筆記(2)調試模式,配置項C,創建模塊, 四種URL模式
data 控制器 idt 默認 模式 com index.php 訪問 alt 一、調試模式 TP的調試模式其實就控制了TP關於配置信息以及函數的緩存功能 如果開啟了調試模式,每次訪問項目,Tp都會去加載最新的配置以及函數信息。 如果關閉了調試模式,當tp第一次訪問時會降配
Part 2 - Fundamentals(4-10)
rip tree mod () pre code python shell url https://simpleisbetterthancomplex.com/series/2017/09/11/a-complete-beginners-guide-to-django-p