轻松把玩HttpClient之封装HttpClient工具类(三),插件式配置Header

上篇文章介绍了插件式配置HttpClient,本文将介绍插件式配置Header。

       为什么要配置header在前面已经提到了,还里再简单说一下,要使用HttpClient模拟请求,去访问各种接口或者网站资源,都有可能有各种限制,比如说java客户端模拟访问csdn博客,就必须设置User-Agent,否则就报错了。还有各种其他情况,必须的设置一些特定的Header,才能请求成功,或者才能不出问题。

       好了就说这么多,本次还是采用构造者模式的级联调用方式,来完成该工具类。在该工具类中,为所有常用的Http Request Header都提供了设置方法。具体参数参考的链接是HTTP Header 详解

       不再多废话了,看具体代码吧:

[java]  view plain  copy
  1. <span style="font-size:18px;">package com.tgb.ccl.http.common;  
  2.   
  3. import java.util.HashMap;  
  4. import java.util.Map;  
  5.   
  6. import org.apache.http.Consts;  
  7. import org.apache.http.Header;  
  8. import org.apache.http.message.BasicHeader;  
  9.   
  10. /** 
  11.  * 创建HttpReqHead 
  12.  *  
  13.  * @author arron 
  14.  * @date 2015年11月9日 上午10:37:23 
  15.  * @version 1.0 
  16.  */  
  17. public class HttpHeader {  
  18.   
  19.     private HttpHeader() {};  
  20.   
  21.     public static HttpHeader custom() {  
  22.         return new HttpHeader();  
  23.     }  
  24.   
  25.     //记录head头信息  
  26.     private Map<String, Header> headerMaps = new HashMap<String, Header>();  
  27.       
  28.     /** 
  29.      * 指定客户端能够接收的内容类型 
  30.      * 例如:Accept: text/plain, text/html 
  31.      *  
  32.      * @param accept 
  33.      */  
  34.     public HttpHeader accept(String accept) {  
  35.         headerMaps.put(HttpReqHead.ACCEPT,  
  36.                 new BasicHeader(HttpReqHead.ACCEPT, accept));  
  37.         return this;  
  38.     }  
  39.   
  40.     /** 
  41.      * 浏览器可以接受的字符编码集 
  42.      * 例如:Accept-Charset: iso-8859-5 
  43.      *  
  44.      * @param acceptCharset 
  45.      */  
  46.     public HttpHeader acceptCharset(String acceptCharset) {  
  47.         headerMaps.put(HttpReqHead.ACCEPT_CHARSET,  
  48.                 new BasicHeader(HttpReqHead.ACCEPT_CHARSET, acceptCharset));  
  49.         return this;  
  50.     }  
  51.   
  52.     /** 
  53.      * 指定浏览器可以支持的web服务器返回内容压缩编码类型 
  54.      * 例如:Accept-Encoding: compress, gzip 
  55.      *  
  56.      * @param acceptEncoding 
  57.      */  
  58.     public HttpHeader acceptEncoding(String acceptEncoding) {  
  59.         headerMaps.put(HttpReqHead.ACCEPT_ENCODING,  
  60.                 new BasicHeader(HttpReqHead.ACCEPT_ENCODING, acceptEncoding));  
  61.         return this;  
  62.     }  
  63.   
  64.     /** 
  65.      * 浏览器可接受的语言 
  66.      * 例如:Accept-Language: en,zh 
  67.      *  
  68.      * @param acceptLanguage 
  69.      */  
  70.     public HttpHeader acceptLanguage(String acceptLanguage) {  
  71.         headerMaps.put(HttpReqHead.ACCEPT_LANGUAGE,  
  72.                 new BasicHeader(HttpReqHead.ACCEPT_LANGUAGE, acceptLanguage));  
  73.         return this;  
  74.     }  
  75.   
  76.     /** 
  77.      * 可以请求网页实体的一个或者多个子范围字段 
  78.      * 例如:Accept-Ranges: bytes 
  79.      *  
  80.      * @param acceptRanges 
  81.      */  
  82.     public HttpHeader acceptRanges(String acceptRanges) {  
  83.         headerMaps.put(HttpReqHead.ACCEPT_RANGES,  
  84.                 new BasicHeader(HttpReqHead.ACCEPT_RANGES, acceptRanges));  
  85.         return this;  
  86.     }  
  87.   
  88.     /** 
  89.      * HTTP授权的授权证书 
  90.      * 例如:Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ== 
  91.      *  
  92.      * @param authorization 
  93.      */  
  94.     public HttpHeader authorization(String authorization) {  
  95.         headerMaps.put(HttpReqHead.AUTHORIZATION,  
  96.                 new BasicHeader(HttpReqHead.AUTHORIZATION, authorization));  
  97.         return this;  
  98.     }  
  99.   
  100.     /** 
  101.      * 指定请求和响应遵循的缓存机制 
  102.      * 例如:Cache-Control: no-cache 
  103.      *  
  104.      * @param cacheControl 
  105.      */  
  106.     public HttpHeader cacheControl(String cacheControl) {  
  107.         headerMaps.put(HttpReqHead.CACHE_CONTROL,  
  108.                 new BasicHeader(HttpReqHead.CACHE_CONTROL, cacheControl));  
  109.         return this;  
  110.     }  
  111.   
  112.     /** 
  113.      * 表示是否需要持久连接(HTTP 1.1默认进行持久连接) 
  114.      * 例如:Connection: close 短链接; Connection: keep-alive 长连接 
  115.      *  
  116.      * @param connection 
  117.      * @return 
  118.      */  
  119.     public HttpHeader connection(String connection) {  
  120.         headerMaps.put(HttpReqHead.CONNECTION,  
  121.                 new BasicHeader(HttpReqHead.CONNECTION, connection));  
  122.         return this;  
  123.     }  
  124.       
  125.     /** 
  126.      * HTTP请求发送时,会把保存在该请求域名下的所有cookie值一起发送给web服务器 
  127.      * 例如:Cookie: $Version=1; Skin=new; 
  128.      *  
  129.      * @param cookie 
  130.      */  
  131.     public HttpHeader cookie(String cookie) {  
  132.         headerMaps.put(HttpReqHead.COOKIE,  
  133.                 new BasicHeader(HttpReqHead.COOKIE, cookie));  
  134.         return this;  
  135.     }  
  136.   
  137.     /** 
  138.      * 请求内容长度 
  139.      * 例如:Content-Length: 348 
  140.      *  
  141.      * @param contentLength 
  142.      */  
  143.     public HttpHeader contentLength(String contentLength) {  
  144.         headerMaps.put(HttpReqHead.CONTENT_LENGTH,  
  145.                 new BasicHeader(HttpReqHead.CONTENT_LENGTH, contentLength));  
  146.         return this;  
  147.     }  
  148.   
  149.     /** 
  150.      * 请求的与实体对应的MIME信息 
  151.      * 例如:Content-Type: application/x-www-form-urlencoded 
  152.      *  
  153.      * @param contentType 
  154.      */  
  155.     public HttpHeader contentType(String contentType) {  
  156.         headerMaps.put(HttpReqHead.CONTENT_TYPE,  
  157.                 new BasicHeader(HttpReqHead.CONTENT_TYPE, contentType));  
  158.         return this;  
  159.     }  
  160.   
  161.     /** 
  162.      * 请求发送的日期和时间 
  163.      * 例如:Date: Tue, 15 Nov 2010 08:12:31 GMT 
  164.      *  
  165.      * @param date 
  166.      * @return 
  167.      */  
  168.     public HttpHeader date(String date) {  
  169.         headerMaps.put(HttpReqHead.DATE,  
  170.                 new BasicHeader(HttpReqHead.DATE, date));  
  171.         return this;  
  172.     }  
  173.       
  174.     /** 
  175.      * 请求的特定的服务器行为 
  176.      * 例如:Expect: 100-continue 
  177.      *  
  178.      * @param expect 
  179.      */  
  180.     public HttpHeader expect(String expect) {  
  181.         headerMaps.put(HttpReqHead.EXPECT,  
  182.                 new BasicHeader(HttpReqHead.EXPECT, expect));  
  183.         return this;  
  184.     }  
  185.       
  186.     /** 
  187.      * 发出请求的用户的Email 
  188.      * 例如:From: user@email.com 
  189.      *  
  190.      * @param from 
  191.      */  
  192.     public HttpHeader from(String from) {  
  193.         headerMaps.put(HttpReqHead.FROM,  
  194.                 new BasicHeader(HttpReqHead.FROM, from));  
  195.         return this;  
  196.     }  
  197.       
  198.     /** 
  199.      * 指定请求的服务器的域名和端口号 
  200.      * 例如:Host: blog.csdn.net 
  201.      *  
  202.      * @param host 
  203.      * @return 
  204.      */  
  205.     public HttpHeader host(String host) {  
  206.         headerMaps.put(HttpReqHead.HOST,  
  207.                 new BasicHeader(HttpReqHead.HOST, host));  
  208.         return this;  
  209.     }  
  210.       
  211.     /** 
  212.      * 只有请求内容与实体相匹配才有效 
  213.      * 例如:If-Match: “737060cd8c284d8af7ad3082f209582d” 
  214.      *  
  215.      * @param ifMatch 
  216.      * @return 
  217.      */  
  218.     public HttpHeader ifMatch(String ifMatch) {  
  219.         headerMaps.put(HttpReqHead.IF_MATCH,  
  220.                 new BasicHeader(HttpReqHead.IF_MATCH, ifMatch));  
  221.         return this;  
  222.     }  
  223.       
  224.     /** 
  225.      * 如果请求的部分在指定时间之后被修改则请求成功,未被修改则返回304代码 
  226.      * 例如:If-Modified-Since: Sat, 29 Oct 2010 19:43:31 GMT 
  227.      *  
  228.      * @param ifModifiedSince 
  229.      * @return 
  230.      */  
  231.     public HttpHeader ifModifiedSince(String ifModifiedSince) {  
  232.         headerMaps.put(HttpReqHead.IF_MODIFIED_SINCE,  
  233.                 new BasicHeader(HttpReqHead.IF_MODIFIED_SINCE, ifModifiedSince));  
  234.         return this;  
  235.     }  
  236.       
  237.     /** 
  238.      * 如果内容未改变返回304代码,参数为服务器先前发送的Etag,与服务器回应的Etag比较判断是否改变 
  239.      * 例如:If-None-Match: “737060cd8c284d8af7ad3082f209582d” 
  240.      *  
  241.      * @param ifNoneMatch 
  242.      * @return 
  243.      */  
  244.     public HttpHeader ifNoneMatch(String ifNoneMatch) {  
  245.         headerMaps.put(HttpReqHead.IF_NONE_MATCH,  
  246.                 new BasicHeader(HttpReqHead.IF_NONE_MATCH, ifNoneMatch));  
  247.         return this;  
  248.     }  
  249.       
  250.     /** 
  251.      * 如果实体未改变,服务器发送客户端丢失的部分,否则发送整个实体。参数也为Etag 
  252.      * 例如:If-Range: “737060cd8c284d8af7ad3082f209582d” 
  253.      *  
  254.      * @param ifRange 
  255.      * @return 
  256.      */  
  257.     public HttpHeader ifRange(String ifRange) {  
  258.         headerMaps.put(HttpReqHead.IF_RANGE,  
  259.                 new BasicHeader(HttpReqHead.IF_RANGE, ifRange));  
  260.         return this;  
  261.     }  
  262.       
  263.     /** 
  264.      * 只在实体在指定时间之后未被修改才请求成功 
  265.      * 例如:If-Unmodified-Since: Sat, 29 Oct 2010 19:43:31 GMT 
  266.      *  
  267.      * @param ifUnmodifiedSince 
  268.      * @return 
  269.      */  
  270.     public HttpHeader ifUnmodifiedSince(String ifUnmodifiedSince) {  
  271.         headerMaps.put(HttpReqHead.IF_UNMODIFIED_SINCE,  
  272.                 new BasicHeader(HttpReqHead.IF_UNMODIFIED_SINCE, ifUnmodifiedSince));  
  273.         return this;  
  274.     }  
  275.       
  276.     /** 
  277.      * 限制信息通过代理和网关传送的时间 
  278.      * 例如:Max-Forwards: 10 
  279.      *  
  280.      * @param maxForwards 
  281.      * @return 
  282.      */  
  283.     public HttpHeader maxForwards(String maxForwards) {  
  284.         headerMaps.put(HttpReqHead.MAX_FORWARDS,  
  285.                 new BasicHeader(HttpReqHead.MAX_FORWARDS, maxForwards));  
  286.         return this;  
  287.     }  
  288.       
  289.     /** 
  290.      * 用来包含实现特定的指令 
  291.      * 例如:Pragma: no-cache 
  292.      *  
  293.      * @param pragma 
  294.      * @return 
  295.      */  
  296.     public HttpHeader pragma(String pragma) {  
  297.         headerMaps.put(HttpReqHead.PRAGMA,  
  298.                 new BasicHeader(HttpReqHead.PRAGMA, pragma));  
  299.         return this;  
  300.     }  
  301.       
  302.     /** 
  303.      * 连接到代理的授权证书 
  304.      * 例如:Proxy-Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ== 
  305.      *  
  306.      * @param proxyAuthorization 
  307.      */  
  308.     public HttpHeader proxyAuthorization(String proxyAuthorization) {  
  309.         headerMaps.put(HttpReqHead.PROXY_AUTHORIZATION,  
  310.                 new BasicHeader(HttpReqHead.PROXY_AUTHORIZATION, proxyAuthorization));  
  311.         return this;  
  312.     }  
  313.       
  314.     /** 
  315.      * 只请求实体的一部分,指定范围 
  316.      * 例如:Range: bytes=500-999 
  317.      *  
  318.      * @param range 
  319.      */  
  320.     public HttpHeader range(String range) {  
  321.         headerMaps.put(HttpReqHead.RANGE,  
  322.                 new BasicHeader(HttpReqHead.RANGE, range));  
  323.         return this;  
  324.     }  
  325.   
  326.     /** 
  327.      * 先前网页的地址,当前请求网页紧随其后,即来路 
  328.      * 例如:Referer: http://www.zcmhi.com/archives/71.html 
  329.      *  
  330.      * @param referer 
  331.      */  
  332.     public HttpHeader referer(String referer) {  
  333.         headerMaps.put(HttpReqHead.REFERER,  
  334.                 new BasicHeader(HttpReqHead.REFERER, referer));  
  335.         return this;  
  336.     }  
  337.       
  338.     /** 
  339.      * 客户端愿意接受的传输编码,并通知服务器接受接受尾加头信息 
  340.      * 例如:TE: trailers,deflate;q=0.5 
  341.      *  
  342.      * @param te 
  343.      */  
  344.     public HttpHeader te(String te) {  
  345.         headerMaps.put(HttpReqHead.TE,  
  346.                 new BasicHeader(HttpReqHead.TE, te));  
  347.         return this;  
  348.     }  
  349.       
  350.     /** 
  351.      * 向服务器指定某种传输协议以便服务器进行转换(如果支持) 
  352.      * 例如:Upgrade: HTTP/2.0, SHTTP/1.3, IRC/6.9, RTA/x11 
  353.      *  
  354.      * @param upgrade 
  355.      */  
  356.     public HttpHeader upgrade(String upgrade) {  
  357.         headerMaps.put(HttpReqHead.UPGRADE,  
  358.                 new BasicHeader(HttpReqHead.UPGRADE, upgrade));  
  359.         return this;  
  360.     }  
  361.       
  362.     /** 
  363.      * User-Agent的内容包含发出请求的用户信息 
  364.      *  
  365.      * @param userAgent 
  366.      * @return 
  367.      */  
  368.     public HttpHeader userAgent(String userAgent) {  
  369.         headerMaps.put(HttpReqHead.USER_AGENT,  
  370.                 new BasicHeader(HttpReqHead.USER_AGENT, userAgent));  
  371.         return this;  
  372.     }  
  373.   
  374.     /** 
  375.      * 关于消息实体的警告信息 
  376.      * 例如:Warn: 199 Miscellaneous warning 
  377.      *  
  378.      * @param warning 
  379.      * @return 
  380.      */  
  381.     public HttpHeader warning(String warning) {  
  382.         headerMaps.put(HttpReqHead.WARNING,  
  383.                 new BasicHeader(HttpReqHead.WARNING, warning));  
  384.         return this;  
  385.     }  
  386.       
  387.     /** 
  388.      * 通知中间网关或代理服务器地址,通信协议 
  389.      * 例如:Via: 1.0 fred, 1.1 nowhere.com (Apache/1.1) 
  390.      *  
  391.      * @param via 
  392.      * @return 
  393.      */  
  394.     public HttpHeader via(String via) {  
  395.         headerMaps.put(HttpReqHead.VIA,  
  396.                 new BasicHeader(HttpReqHead.VIA, via));  
  397.         return this;  
  398.     }  
  399.   
  400.     /** 
  401.      * 设置此HTTP连接的持续时间(超时时间) 
  402.      * 例如:Keep-Alive: 300 
  403.      *  
  404.      * @param keepAlive 
  405.      * @return 
  406.      */  
  407.     public HttpHeader keepAlive(String keepAlive) {  
  408.         headerMaps.put(HttpReqHead.KEEP_ALIVE,  
  409.                 new BasicHeader(HttpReqHead.KEEP_ALIVE, keepAlive));  
  410.         return this;  
  411.     }  
  412.   
  413.     public String accept() {  
  414.         return get(HttpReqHead.ACCEPT);  
  415.     }  
  416.   
  417.     public String acceptCharset() {  
  418.         return get(HttpReqHead.ACCEPT_CHARSET);  
  419.     }  
  420.   
  421.     public String acceptEncoding() {  
  422.         return get(HttpReqHead.ACCEPT_ENCODING);  
  423.     }  
  424.   
  425.     public String acceptLanguage() {  
  426.         return get(HttpReqHead.ACCEPT_LANGUAGE);  
  427.     }  
  428.   
  429.     public String acceptRanges() {  
  430.         return get(HttpReqHead.ACCEPT_RANGES);  
  431.     }  
  432.   
  433.     public String authorization() {  
  434.         return get(HttpReqHead.AUTHORIZATION);  
  435.     }  
  436.   
  437.     public String cacheControl() {  
  438.         return get(HttpReqHead.CACHE_CONTROL);  
  439.     }  
  440.   
  441.     public String connection() {  
  442.         return get(HttpReqHead.CONNECTION);  
  443.     }  
  444.       
  445.     public String cookie() {  
  446.         return get(HttpReqHead.COOKIE);  
  447.     }  
  448.   
  449.     public String contentLength() {  
  450.         return get(HttpReqHead.CONTENT_LENGTH);  
  451.     }  
  452.   
  453.     public String contentType() {  
  454.         return get(HttpReqHead.CONTENT_TYPE);  
  455.     }  
  456.       
  457.     public String date() {  
  458.         return get(HttpReqHead.DATE);  
  459.     }  
  460.   
  461.     public String expect() {  
  462.         return get(HttpReqHead.EXPECT);  
  463.     }  
  464.   
  465.     public String from() {  
  466.         return get(HttpReqHead.FROM);  
  467.     }  
  468.       
  469.     public String host() {  
  470.         return get(HttpReqHead.HOST);  
  471.     }  
  472.       
  473.     public String ifMatch() {  
  474.         return get(HttpReqHead.IF_MATCH);  
  475.     }  
  476.       
  477.     public String ifModifiedSince() {  
  478.         return get(HttpReqHead.IF_MODIFIED_SINCE);  
  479.     }  
  480.       
  481.     public String ifNoneMatch() {  
  482.         return get(HttpReqHead.IF_NONE_MATCH);  
  483.     }  
  484.       
  485.     public String ifRange() {  
  486.         return get(HttpReqHead.IF_RANGE);  
  487.     }  
  488.       
  489.     public String ifUnmodifiedSince() {  
  490.         return get(HttpReqHead.IF_UNMODIFIED_SINCE);  
  491.     }  
  492.       
  493.     public String maxForwards() {  
  494.         return get(HttpReqHead.MAX_FORWARDS);  
  495.     }  
  496.       
  497.     public String pragma() {  
  498.         return get(HttpReqHead.PRAGMA);  
  499.     }  
  500.   
  501.     public String proxyAuthorization() {  
  502.         return get(HttpReqHead.PROXY_AUTHORIZATION);  
  503.     }  
  504.   
  505.     public String referer() {  
  506.         return get(HttpReqHead.REFERER);  
  507.     }  
  508.       
  509.     public String te() {  
  510.         return get(HttpReqHead.TE);  
  511.     }  
  512.       
  513.     public String upgrade() {  
  514.         return get(HttpReqHead.UPGRADE);  
  515.     }  
  516.       
  517.     public String userAgent() {  
  518.         return get(HttpReqHead.USER_AGENT);  
  519.     }  
  520.       
  521.     public String via() {  
  522.         return get(HttpReqHead.VIA);  
  523.     }  
  524.       
  525.     public String warning() {  
  526.         return get(HttpReqHead.WARNING);  
  527.     }  
  528.   
  529.     public String keepAlive() {  
  530.         return get(HttpReqHead.KEEP_ALIVE);  
  531.     }  
  532.       
  533.       
  534.     /** 
  535.      * 获取head信息 
  536.      *  
  537.      * @return 
  538.      */  
  539.     private String get(String headName) {  
  540.         if (headerMaps.containsKey(headName)) {  
  541.             return headerMaps.get(headName).getValue();  
  542.         }  
  543.         return null;  
  544.     }  
  545.   
  546.     /** 
  547.      * 返回header头信息 
  548.      *  
  549.      * @return 
  550.      */  
  551.     public Header[] build() {  
  552.         Header[] headers = new Header[headerMaps.size()];  
  553.         int i = 0;  
  554.         for (Header header : headerMaps.values()) {  
  555.             headers[i] = header;  
  556.             i++;  
  557.         }  
  558.         headerMaps.clear();  
  559.         headerMaps = null;  
  560.         return headers;  
  561.     }  
  562.   
  563.     /** 
  564.      * Http头信息 
  565.      *  
  566.      * @author arron 
  567.      * @date 2015年11月9日 上午11:29:04 
  568.      * @version 1.0 
  569.      */  
  570.     private static class HttpReqHead {  
  571.         public static final String ACCEPT = "Accept";  
  572.         public static final String ACCEPT_CHARSET = "Accept-Charset";  
  573.         public static final String ACCEPT_ENCODING = "Accept-Encoding";  
  574.         public static final String ACCEPT_LANGUAGE = "Accept-Language";  
  575.         public static final String ACCEPT_RANGES = "Accept-Ranges";  
  576.         public static final String AUTHORIZATION = "Authorization";  
  577.         public static final String CACHE_CONTROL = "Cache-Control";  
  578.         public static final String CONNECTION = "Connection";  
  579.         public static final String COOKIE = "Cookie";  
  580.         public static final String CONTENT_LENGTH = "Content-Length";  
  581.         public static final String CONTENT_TYPE = "Content-Type";  
  582.         public static final String DATE= "Date";  
  583.         public static final String EXPECT = "Expect";  
  584.         public static final String FROM = "From";  
  585.         public static final String HOST = "Host";  
  586.         public static final String IF_MATCH = "If-Match ";  
  587.         public static final String IF_MODIFIED_SINCE = "If-Modified-Since";  
  588.         public static final String IF_NONE_MATCH = "If-None-Match";  
  589.         public static final String IF_RANGE = "If-Range";  
  590.         public static final String IF_UNMODIFIED_SINCE = "If-Unmodified-Since";  
  591.         public static final String KEEP_ALIVE = "Keep-Alive";  
  592.         public static final String MAX_FORWARDS = "Max-Forwards";  
  593.         public static final String PRAGMA = "Pragma";  
  594.         public static final String PROXY_AUTHORIZATION = "Proxy-Authorization";  
  595.         public static final String RANGE = "Range";  
  596.         public static final String REFERER = "Referer";  
  597.         public static final String TE = "TE";  
  598.         public static final String UPGRADE = "Upgrade";  
  599.         public static final String USER_AGENT = "User-Agent";  
  600.         public static final String VIA = "Via";  
  601.         public static final String WARNING = "Warning";  
  602.     }  
  603.       
  604.     /** 
  605.      * 常用头信息配置 
  606.      *  
  607.      * @author arron 
  608.      * @date 2015年11月18日 下午5:30:00  
  609.      * @version 1.0 
  610.      */  
  611.     public static class Headers{  
  612.         public static final String APP_FORM_URLENCODED="application/x-www-form-urlencoded";  
  613.         public static final String TEXT_PLAIN="text/plain";  
  614.         public static final String TEXT_HTML="text/html";  
  615.         public static final String TEXT_XML="text/xml";  
  616.         public static final String TEXT_JSON="text/json";  
  617.         public static final String CONTENT_CHARSET_ISO_8859_1 = Consts.ISO_8859_1.name();  
  618.         public static final String CONTENT_CHARSET_UTF8 = Consts.UTF_8.name();  
  619.         public static final String DEF_PROTOCOL_CHARSET = Consts.ASCII.name();  
  620.         public static final String CONN_CLOSE = "close";  
  621.         public static final String KEEP_ALIVE = "keep-alive";  
  622.         public static final String EXPECT_CONTINUE = "100-continue";  
  623.     }  
  624. }  
  625. </span>  
       调用方式:
[java]  view plain  copy
  1. <span style="font-size:18px;">  //设置header信息  
  2.     Header[] headers=HttpHeader.custom().keepAlive("false").connection("close").contentType(Headers.APP_FORM_URLENCODED).build();</span>  
       就是这么简单。到此该工具类就完成了。下一篇将分享该工具类以及单次调用测试和多线程调用测试。

       代码已上传https://github.com/Arronlong/httpclientUtil

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值