? ? ?版權聲明:本文為博主原創文章,未經博主允許不得轉載。如需轉載請聲明:【轉自 http://blog.csdn.net/xiaoxian8023 】
? ? ?上篇文章介紹了插件式配置HttpClient,本文將介紹插件式配置Header。
? ? ?為什么要配置header在前面已經提到了,還里再簡單說一下,要使用HttpClient模擬請求,去訪問各種接口或者網站資源,都有可能有各種限制,比如說java客戶端模擬訪問csdn博客,就必須設置User-Agent,否則就報錯了。還有各種其他情況,必須的設置一些特定的Header,才能請求成功,或者才能不出問題。
? ? ?好了就說這么多,本次還是采用構造者模式的級聯調用方式,來完成該工具類。在該工具類中,為所有常用的Http Request Header都提供了設置方法。具體參數參考的鏈接是[HTTP Header 詳解](http://kb.cnblogs.com/page/92320/)。
? ? ?不再多廢話了,看具體代碼吧:
~~~
package com.tgb.ccl.http.common;
import java.util.HashMap;
import java.util.Map;
import org.apache.http.Consts;
import org.apache.http.Header;
import org.apache.http.message.BasicHeader;
/**
* 創建HttpReqHead
*
* @author arron
* @date 2015年11月9日 上午10:37:23
* @version 1.0
*/
public class HttpHeader {
private HttpHeader() {};
public static HttpHeader custom() {
return new HttpHeader();
}
//記錄head頭信息
private Map<String, Header> headerMaps = new HashMap<String, Header>();
/**
* 指定客戶端能夠接收的內容類型
* 例如:Accept: text/plain, text/html
*
* @param accept
*/
public HttpHeader accept(String accept) {
headerMaps.put(HttpReqHead.ACCEPT,
new BasicHeader(HttpReqHead.ACCEPT, accept));
return this;
}
/**
* 瀏覽器可以接受的字符編碼集
* 例如:Accept-Charset: iso-8859-5
*
* @param acceptCharset
*/
public HttpHeader acceptCharset(String acceptCharset) {
headerMaps.put(HttpReqHead.ACCEPT_CHARSET,
new BasicHeader(HttpReqHead.ACCEPT_CHARSET, acceptCharset));
return this;
}
/**
* 指定瀏覽器可以支持的web服務器返回內容壓縮編碼類型
* 例如:Accept-Encoding: compress, gzip
*
* @param acceptEncoding
*/
public HttpHeader acceptEncoding(String acceptEncoding) {
headerMaps.put(HttpReqHead.ACCEPT_ENCODING,
new BasicHeader(HttpReqHead.ACCEPT_ENCODING, acceptEncoding));
return this;
}
/**
* 瀏覽器可接受的語言
* 例如:Accept-Language: en,zh
*
* @param acceptLanguage
*/
public HttpHeader acceptLanguage(String acceptLanguage) {
headerMaps.put(HttpReqHead.ACCEPT_LANGUAGE,
new BasicHeader(HttpReqHead.ACCEPT_LANGUAGE, acceptLanguage));
return this;
}
/**
* 可以請求網頁實體的一個或者多個子范圍字段
* 例如:Accept-Ranges: bytes
*
* @param acceptRanges
*/
public HttpHeader acceptRanges(String acceptRanges) {
headerMaps.put(HttpReqHead.ACCEPT_RANGES,
new BasicHeader(HttpReqHead.ACCEPT_RANGES, acceptRanges));
return this;
}
/**
* HTTP授權的授權證書
* 例如:Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
*
* @param authorization
*/
public HttpHeader authorization(String authorization) {
headerMaps.put(HttpReqHead.AUTHORIZATION,
new BasicHeader(HttpReqHead.AUTHORIZATION, authorization));
return this;
}
/**
* 指定請求和響應遵循的緩存機制
* 例如:Cache-Control: no-cache
*
* @param cacheControl
*/
public HttpHeader cacheControl(String cacheControl) {
headerMaps.put(HttpReqHead.CACHE_CONTROL,
new BasicHeader(HttpReqHead.CACHE_CONTROL, cacheControl));
return this;
}
/**
* 表示是否需要持久連接(HTTP 1.1默認進行持久連接)
* 例如:Connection: close 短鏈接; Connection: keep-alive 長連接
*
* @param connection
* @return
*/
public HttpHeader connection(String connection) {
headerMaps.put(HttpReqHead.CONNECTION,
new BasicHeader(HttpReqHead.CONNECTION, connection));
return this;
}
/**
* HTTP請求發送時,會把保存在該請求域名下的所有cookie值一起發送給web服務器
* 例如:Cookie: $Version=1; Skin=new;
*
* @param cookie
*/
public HttpHeader cookie(String cookie) {
headerMaps.put(HttpReqHead.COOKIE,
new BasicHeader(HttpReqHead.COOKIE, cookie));
return this;
}
/**
* 請求內容長度
* 例如:Content-Length: 348
*
* @param contentLength
*/
public HttpHeader contentLength(String contentLength) {
headerMaps.put(HttpReqHead.CONTENT_LENGTH,
new BasicHeader(HttpReqHead.CONTENT_LENGTH, contentLength));
return this;
}
/**
* 請求的與實體對應的MIME信息
* 例如:Content-Type: application/x-www-form-urlencoded
*
* @param contentType
*/
public HttpHeader contentType(String contentType) {
headerMaps.put(HttpReqHead.CONTENT_TYPE,
new BasicHeader(HttpReqHead.CONTENT_TYPE, contentType));
return this;
}
/**
* 請求發送的日期和時間
* 例如:Date: Tue, 15 Nov 2010 08:12:31 GMT
*
* @param date
* @return
*/
public HttpHeader date(String date) {
headerMaps.put(HttpReqHead.DATE,
new BasicHeader(HttpReqHead.DATE, date));
return this;
}
/**
* 請求的特定的服務器行為
* 例如:Expect: 100-continue
*
* @param expect
*/
public HttpHeader expect(String expect) {
headerMaps.put(HttpReqHead.EXPECT,
new BasicHeader(HttpReqHead.EXPECT, expect));
return this;
}
/**
* 發出請求的用戶的Email
* 例如:From: user@email.com
*
* @param from
*/
public HttpHeader from(String from) {
headerMaps.put(HttpReqHead.FROM,
new BasicHeader(HttpReqHead.FROM, from));
return this;
}
/**
* 指定請求的服務器的域名和端口號
* 例如:Host: blog.csdn.net
*
* @param host
* @return
*/
public HttpHeader host(String host) {
headerMaps.put(HttpReqHead.HOST,
new BasicHeader(HttpReqHead.HOST, host));
return this;
}
/**
* 只有請求內容與實體相匹配才有效
* 例如:If-Match: “737060cd8c284d8af7ad3082f209582d”
*
* @param ifMatch
* @return
*/
public HttpHeader ifMatch(String ifMatch) {
headerMaps.put(HttpReqHead.IF_MATCH,
new BasicHeader(HttpReqHead.IF_MATCH, ifMatch));
return this;
}
/**
* 如果請求的部分在指定時間之后被修改則請求成功,未被修改則返回304代碼
* 例如:If-Modified-Since: Sat, 29 Oct 2010 19:43:31 GMT
*
* @param ifModifiedSince
* @return
*/
public HttpHeader ifModifiedSince(String ifModifiedSince) {
headerMaps.put(HttpReqHead.IF_MODIFIED_SINCE,
new BasicHeader(HttpReqHead.IF_MODIFIED_SINCE, ifModifiedSince));
return this;
}
/**
* 如果內容未改變返回304代碼,參數為服務器先前發送的Etag,與服務器回應的Etag比較判斷是否改變
* 例如:If-None-Match: “737060cd8c284d8af7ad3082f209582d”
*
* @param ifNoneMatch
* @return
*/
public HttpHeader ifNoneMatch(String ifNoneMatch) {
headerMaps.put(HttpReqHead.IF_NONE_MATCH,
new BasicHeader(HttpReqHead.IF_NONE_MATCH, ifNoneMatch));
return this;
}
/**
* 如果實體未改變,服務器發送客戶端丟失的部分,否則發送整個實體。參數也為Etag
* 例如:If-Range: “737060cd8c284d8af7ad3082f209582d”
*
* @param ifRange
* @return
*/
public HttpHeader ifRange(String ifRange) {
headerMaps.put(HttpReqHead.IF_RANGE,
new BasicHeader(HttpReqHead.IF_RANGE, ifRange));
return this;
}
/**
* 只在實體在指定時間之后未被修改才請求成功
* 例如:If-Unmodified-Since: Sat, 29 Oct 2010 19:43:31 GMT
*
* @param ifUnmodifiedSince
* @return
*/
public HttpHeader ifUnmodifiedSince(String ifUnmodifiedSince) {
headerMaps.put(HttpReqHead.IF_UNMODIFIED_SINCE,
new BasicHeader(HttpReqHead.IF_UNMODIFIED_SINCE, ifUnmodifiedSince));
return this;
}
/**
* 限制信息通過代理和網關傳送的時間
* 例如:Max-Forwards: 10
*
* @param maxForwards
* @return
*/
public HttpHeader maxForwards(String maxForwards) {
headerMaps.put(HttpReqHead.MAX_FORWARDS,
new BasicHeader(HttpReqHead.MAX_FORWARDS, maxForwards));
return this;
}
/**
* 用來包含實現特定的指令
* 例如:Pragma: no-cache
*
* @param pragma
* @return
*/
public HttpHeader pragma(String pragma) {
headerMaps.put(HttpReqHead.PRAGMA,
new BasicHeader(HttpReqHead.PRAGMA, pragma));
return this;
}
/**
* 連接到代理的授權證書
* 例如:Proxy-Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
*
* @param proxyAuthorization
*/
public HttpHeader proxyAuthorization(String proxyAuthorization) {
headerMaps.put(HttpReqHead.PROXY_AUTHORIZATION,
new BasicHeader(HttpReqHead.PROXY_AUTHORIZATION, proxyAuthorization));
return this;
}
/**
* 只請求實體的一部分,指定范圍
* 例如:Range: bytes=500-999
*
* @param range
*/
public HttpHeader range(String range) {
headerMaps.put(HttpReqHead.RANGE,
new BasicHeader(HttpReqHead.RANGE, range));
return this;
}
/**
* 先前網頁的地址,當前請求網頁緊隨其后,即來路
* 例如:Referer: http://www.zcmhi.com/archives/71.html
*
* @param referer
*/
public HttpHeader referer(String referer) {
headerMaps.put(HttpReqHead.REFERER,
new BasicHeader(HttpReqHead.REFERER, referer));
return this;
}
/**
* 客戶端愿意接受的傳輸編碼,并通知服務器接受接受尾加頭信息
* 例如:TE: trailers,deflate;q=0.5
*
* @param te
*/
public HttpHeader te(String te) {
headerMaps.put(HttpReqHead.TE,
new BasicHeader(HttpReqHead.TE, te));
return this;
}
/**
* 向服務器指定某種傳輸協議以便服務器進行轉換(如果支持)
* 例如:Upgrade: HTTP/2.0, SHTTP/1.3, IRC/6.9, RTA/x11
*
* @param upgrade
*/
public HttpHeader upgrade(String upgrade) {
headerMaps.put(HttpReqHead.UPGRADE,
new BasicHeader(HttpReqHead.UPGRADE, upgrade));
return this;
}
/**
* User-Agent的內容包含發出請求的用戶信息
*
* @param userAgent
* @return
*/
public HttpHeader userAgent(String userAgent) {
headerMaps.put(HttpReqHead.USER_AGENT,
new BasicHeader(HttpReqHead.USER_AGENT, userAgent));
return this;
}
/**
* 關于消息實體的警告信息
* 例如:Warn: 199 Miscellaneous warning
*
* @param warning
* @return
*/
public HttpHeader warning(String warning) {
headerMaps.put(HttpReqHead.WARNING,
new BasicHeader(HttpReqHead.WARNING, warning));
return this;
}
/**
* 通知中間網關或代理服務器地址,通信協議
* 例如:Via: 1.0 fred, 1.1 nowhere.com (Apache/1.1)
*
* @param via
* @return
*/
public HttpHeader via(String via) {
headerMaps.put(HttpReqHead.VIA,
new BasicHeader(HttpReqHead.VIA, via));
return this;
}
/**
* 設置此HTTP連接的持續時間(超時時間)
* 例如:Keep-Alive: 300
*
* @param keepAlive
* @return
*/
public HttpHeader keepAlive(String keepAlive) {
headerMaps.put(HttpReqHead.KEEP_ALIVE,
new BasicHeader(HttpReqHead.KEEP_ALIVE, keepAlive));
return this;
}
public String accept() {
return get(HttpReqHead.ACCEPT);
}
public String acceptCharset() {
return get(HttpReqHead.ACCEPT_CHARSET);
}
public String acceptEncoding() {
return get(HttpReqHead.ACCEPT_ENCODING);
}
public String acceptLanguage() {
return get(HttpReqHead.ACCEPT_LANGUAGE);
}
public String acceptRanges() {
return get(HttpReqHead.ACCEPT_RANGES);
}
public String authorization() {
return get(HttpReqHead.AUTHORIZATION);
}
public String cacheControl() {
return get(HttpReqHead.CACHE_CONTROL);
}
public String connection() {
return get(HttpReqHead.CONNECTION);
}
public String cookie() {
return get(HttpReqHead.COOKIE);
}
public String contentLength() {
return get(HttpReqHead.CONTENT_LENGTH);
}
public String contentType() {
return get(HttpReqHead.CONTENT_TYPE);
}
public String date() {
return get(HttpReqHead.DATE);
}
public String expect() {
return get(HttpReqHead.EXPECT);
}
public String from() {
return get(HttpReqHead.FROM);
}
public String host() {
return get(HttpReqHead.HOST);
}
public String ifMatch() {
return get(HttpReqHead.IF_MATCH);
}
public String ifModifiedSince() {
return get(HttpReqHead.IF_MODIFIED_SINCE);
}
public String ifNoneMatch() {
return get(HttpReqHead.IF_NONE_MATCH);
}
public String ifRange() {
return get(HttpReqHead.IF_RANGE);
}
public String ifUnmodifiedSince() {
return get(HttpReqHead.IF_UNMODIFIED_SINCE);
}
public String maxForwards() {
return get(HttpReqHead.MAX_FORWARDS);
}
public String pragma() {
return get(HttpReqHead.PRAGMA);
}
public String proxyAuthorization() {
return get(HttpReqHead.PROXY_AUTHORIZATION);
}
public String referer() {
return get(HttpReqHead.REFERER);
}
public String te() {
return get(HttpReqHead.TE);
}
public String upgrade() {
return get(HttpReqHead.UPGRADE);
}
public String userAgent() {
return get(HttpReqHead.USER_AGENT);
}
public String via() {
return get(HttpReqHead.VIA);
}
public String warning() {
return get(HttpReqHead.WARNING);
}
public String keepAlive() {
return get(HttpReqHead.KEEP_ALIVE);
}
/**
* 獲取head信息
*
* @return
*/
private String get(String headName) {
if (headerMaps.containsKey(headName)) {
return headerMaps.get(headName).getValue();
}
return null;
}
/**
* 返回header頭信息
*
* @return
*/
public Header[] build() {
Header[] headers = new Header[headerMaps.size()];
int i = 0;
for (Header header : headerMaps.values()) {
headers[i] = header;
i++;
}
headerMaps.clear();
headerMaps = null;
return headers;
}
/**
* Http頭信息
*
* @author arron
* @date 2015年11月9日 上午11:29:04
* @version 1.0
*/
private static class HttpReqHead {
public static final String ACCEPT = "Accept";
public static final String ACCEPT_CHARSET = "Accept-Charset";
public static final String ACCEPT_ENCODING = "Accept-Encoding";
public static final String ACCEPT_LANGUAGE = "Accept-Language";
public static final String ACCEPT_RANGES = "Accept-Ranges";
public static final String AUTHORIZATION = "Authorization";
public static final String CACHE_CONTROL = "Cache-Control";
public static final String CONNECTION = "Connection";
public static final String COOKIE = "Cookie";
public static final String CONTENT_LENGTH = "Content-Length";
public static final String CONTENT_TYPE = "Content-Type";
public static final String DATE= "Date";
public static final String EXPECT = "Expect";
public static final String FROM = "From";
public static final String HOST = "Host";
public static final String IF_MATCH = "If-Match ";
public static final String IF_MODIFIED_SINCE = "If-Modified-Since";
public static final String IF_NONE_MATCH = "If-None-Match";
public static final String IF_RANGE = "If-Range";
public static final String IF_UNMODIFIED_SINCE = "If-Unmodified-Since";
public static final String KEEP_ALIVE = "Keep-Alive";
public static final String MAX_FORWARDS = "Max-Forwards";
public static final String PRAGMA = "Pragma";
public static final String PROXY_AUTHORIZATION = "Proxy-Authorization";
public static final String RANGE = "Range";
public static final String REFERER = "Referer";
public static final String TE = "TE";
public static final String UPGRADE = "Upgrade";
public static final String USER_AGENT = "User-Agent";
public static final String VIA = "Via";
public static final String WARNING = "Warning";
}
/**
* 常用頭信息配置
*
* @author arron
* @date 2015年11月18日 下午5:30:00
* @version 1.0
*/
public static class Headers{
public static final String APP_FORM_URLENCODED="application/x-www-form-urlencoded";
public static final String TEXT_PLAIN="text/plain";
public static final String TEXT_HTML="text/html";
public static final String TEXT_XML="text/xml";
public static final String TEXT_JSON="text/json";
public static final String CONTENT_CHARSET_ISO_8859_1 = Consts.ISO_8859_1.name();
public static final String CONTENT_CHARSET_UTF8 = Consts.UTF_8.name();
public static final String DEF_PROTOCOL_CHARSET = Consts.ASCII.name();
public static final String CONN_CLOSE = "close";
public static final String KEEP_ALIVE = "keep-alive";
public static final String EXPECT_CONTINUE = "100-continue";
}
}
~~~
? ? ?調用方式:
~~~
//設置header信息
Header[] headers=HttpHeader.custom().keepAlive("false").connection("close").contentType(Headers.APP_FORM_URLENCODED).build();
~~~
? ? ?就是這么簡單。到此該工具類就完成了。下一篇將分享該工具類以及單次調用測試和多線程調用測試。
? ? ?代碼已上傳至:[https://github.com/Arronlong/httpclientUtil](https://github.com/Arronlong/httpclientUtil)。
- 前言
- HttpClient3.x之Get請求和Post請求示例
- httpclient3.x中使用HTTPS的方法
- 簡單的利用UrlConnection,后臺模擬http請求
- 輕松把玩HttpClient之模擬post請求示例
- 輕松把玩HttpClient之配置ssl,采用繞過證書驗證實現https
- 輕松把玩HttpClient之配置ssl,采用設置信任自簽名證書實現https
- 輕松把玩HttpClient之設置代理,可以訪問FaceBook
- 輕松把玩HttpClient之封裝HttpClient工具類(一)(現有網上分享中的最強大的工具類)
- 輕松把玩HttpClient之封裝HttpClient工具類(二),插件式配置HttpClient對象
- 輕松把玩HttpClient之封裝HttpClient工具類(三),插件式配置Header
- 輕松把玩HttpClient之封裝HttpClient工具類(四),單線程調用及多線程批量調用測試
- 輕松把玩HttpAsyncClient之模擬post請求示例
- 輕松把玩HttpClient之封裝HttpClient工具類(五),攜帶Cookie的請求
- 輕松把玩HttpClient之封裝HttpClient工具類(六),封裝輸入參數,簡化工具類