All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.feilong.net.http.HttpClientUtil Maven / Gradle / Ivy

Go to download

feilong is a suite of core and expanded libraries that include utility classes, http, excel,cvs, io classes, and much much more.

There is a newer version: 4.0.8
Show newest version
/*
  * Copyright (C) 2008 feilong
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.feilong.net.http;

import java.util.Map;

import com.feilong.core.Validate;
import com.feilong.net.http.builder.HttpRequestExecuter;
import com.feilong.net.http.callback.HttpResponseResultCallback;
import com.feilong.net.http.callback.ResponseBodyAsStringResultCallback;
import com.feilong.net.http.callback.StatusCodeResultCallback;

/**
 * 基于 HttpClient4 的工具类.
 *
 * @author feilong
 * @see org.apache.http.client.methods.HttpUriRequest
 * @see "com.feilong.tools.net.httpclient3.HttpClientUtil"
 * @see "org.springframework.http.client.HttpComponentsClientHttpResponse"
 * @see Apache HttpComponents
 * @see QuickStart
 * @since 1.10.6
 */
@SuppressWarnings("squid:S1192") //String literals should not be duplicated
public final class HttpClientUtil{

    /** Don't let anyone instantiate this class. */
    private HttpClientUtil(){
        //AssertionError不是必须的. 但它可以避免不小心在类的内部调用构造器. 保证该类在任何情况下都不会被实例化.
        //see 《Effective Java》 2nd
        throw new AssertionError("No " + getClass().getName() + " instances for you!");
    }

    //---------------------getResponseStatusCode------------------------------------------

    /**
     * 获得请求的响应码.
     * 
     * 

* 默认 {@link HttpMethodType#GET} 请求 *

* *

示例:

* *
* *
     * 
     * public void testGetResponseBodyAsString(){
     *     String urlString = "http://www.baidu.com";
     * 
     *     int responseStatusCode = HttpClientUtil.getResponseStatusCode(urlString);
     *     LOGGER.debug("" + responseStatusCode);
     * }
     * 
     * 
* * 返回: * *
     * 200
     * 
* *
* * @param urlString * the url string * @return 如果 urlString 是null,抛出 {@link NullPointerException}
* 如果 urlString 是blank,抛出 {@link IllegalArgumentException}
*/ public static int getResponseStatusCode(String urlString){ Validate.notBlank(urlString, "urlString can't be blank!"); return getResponseStatusCode(urlString, null); } /** * 获得请求的响应码. * *

* 默认 {@link HttpMethodType#GET} 请求 *

* *

示例:

* *
* *
     * 
     * public void testGetResponseBodyAsString(){
     *     String urlString = "http://www.baidu.com";
     * 
     *     int responseStatusCode = HttpClientUtil.getResponseStatusCode(urlString);
     *     LOGGER.debug("" + responseStatusCode);
     * }
     * 
     * 
* * 返回: * *
     * 200
     * 
* *
* * @param urlString * the url string * @param connectionConfig * the connection config * @return 如果 urlString 是null,抛出 {@link NullPointerException}
* 如果 urlString 是blank,抛出 {@link IllegalArgumentException}
*/ public static int getResponseStatusCode(String urlString,ConnectionConfig connectionConfig){ Validate.notBlank(urlString, "urlString can't be blank!"); HttpRequest httpRequest = new HttpRequest(urlString); return getResponseStatusCode(httpRequest, connectionConfig); } /** * 获得请求的响应码. * *

示例:

* *
* *
     * 
     * public void testGetResponseBodyAsString(){
     *     String urlString = "http://www.baidu.com";
     * 
     *     int responseStatusCode = HttpClientUtil.getResponseStatusCode(urlString);
     *     LOGGER.debug("" + responseStatusCode);
     * }
     * 
     * 
* * 返回: * *
     * 200
     * 
* *
* * @param httpRequest * the http request * @param connectionConfig * the connection config * @return 如果 httpRequest 是null,抛出 {@link NullPointerException}
*/ public static int getResponseStatusCode(HttpRequest httpRequest,ConnectionConfig connectionConfig){ Validate.notNull(httpRequest, "httpRequest can't be null!"); return HttpRequestExecuter.execute(httpRequest, connectionConfig, StatusCodeResultCallback.INSTANCE); } //---------------------------getHttpResponse------------------------------------ /** * 获得 {@link com.feilong.net.http.HttpResponse}. * *

示例:

* *
* *

* 场景: 发请求到百度 *

* *
     * 
     * public void testGetResponseBodyAsString1(){
     *     String urlString = "http://localhost:8081/member/login";
     *     urlString = "http://www.baidu.com";
     * 
     *     HttpClientUtil.getHttpResponse(urlString);
     * }
     * 
* * 返回: * *
    {@code
    
    response:[    {
            "statusCode": 200,
            "resultString": "\r\n  hao123<\/a>  About Baidu<\/a> <\/p> 

©2017 Baidu 使用百度前必读<\/a>  意见反馈<\/a> 京ICP证030173号  <\/p> <\/div> <\/div> <\/div> <\/body> <\/html>\r\n", "useTime": 1721, "headerList": [ { "name": "Cache-Control", "value": "private, no-cache, no-store, proxy-revalidate, no-transform" }, { "name": "Connection", "value": "Keep-Alive" }, { "name": "Content-Type", "value": "text/html" }, { "name": "Date", "value": "Wed, 29 Nov 2017 14:41:32 GMT" }, { "name": "Last-Modified", "value": "Mon, 23 Jan 2017 13:28:36 GMT" }, { "name": "Pragma", "value": "no-cache" }, { "name": "Server", "value": "bfe/1.0.8.18" }, { "name": "Set-Cookie", "value": "BDORZ=27315; max-age=86400; domain=.baidu.com; path=/" }, { "name": "Transfer-Encoding", "value": "chunked" } ] }] } *

* *
* * @param urlString * the url string * @return 如果 urlString 是null,抛出 {@link NullPointerException}
* 如果 urlString 是blank,抛出 {@link IllegalArgumentException}
*/ public static com.feilong.net.http.HttpResponse getHttpResponse(String urlString){ Validate.notBlank(urlString, "urlString can't be blank!"); return getHttpResponse(urlString, null); } /** * 获得 {@link com.feilong.net.http.HttpResponse}. * *

示例:

* *
* *

* 场景: 发请求到百度 *

* *
     * 
     * public void testGetResponseBodyAsString1(){
     *     String urlString = "http://localhost:8081/member/login";
     *     urlString = "http://www.baidu.com";
     * 
     *     HttpClientUtil.getHttpResponse(urlString);
     * }
     * 
* * 返回: * *
    {@code
    
    response:[    {
            "statusCode": 200,
            "resultString": "\r\n  hao123<\/a>  About Baidu<\/a> <\/p> 

©2017 Baidu 使用百度前必读<\/a>  意见反馈<\/a> 京ICP证030173号  <\/p> <\/div> <\/div> <\/div> <\/body> <\/html>\r\n", "useTime": 1721, "headerList": [ { "name": "Cache-Control", "value": "private, no-cache, no-store, proxy-revalidate, no-transform" }, { "name": "Connection", "value": "Keep-Alive" }, { "name": "Content-Type", "value": "text/html" }, { "name": "Date", "value": "Wed, 29 Nov 2017 14:41:32 GMT" }, { "name": "Last-Modified", "value": "Mon, 23 Jan 2017 13:28:36 GMT" }, { "name": "Pragma", "value": "no-cache" }, { "name": "Server", "value": "bfe/1.0.8.18" }, { "name": "Set-Cookie", "value": "BDORZ=27315; max-age=86400; domain=.baidu.com; path=/" }, { "name": "Transfer-Encoding", "value": "chunked" } ] }] } *

* *
* * @param urlString * the url string * @param connectionConfig * the connection config * @return 如果 urlString 是null,抛出 {@link NullPointerException}
* 如果 urlString 是blank,抛出 {@link IllegalArgumentException}
*/ public static com.feilong.net.http.HttpResponse getHttpResponse(String urlString,ConnectionConfig connectionConfig){ Validate.notBlank(urlString, "urlString can't be blank!"); HttpRequest httpRequest = new HttpRequest(urlString); return getHttpResponse(httpRequest, connectionConfig); } /** * 获得 {@link com.feilong.net.http.HttpResponse}. * *

示例:

* *
* *

* 场景: 发请求到百度 *

* *
     * 
     * public void testGetResponseBodyAsString1(){
     *     String urlString = "http://localhost:8081/member/login";
     *     urlString = "http://www.baidu.com";
     * 
     *     HttpClientUtil.getHttpResponse(urlString);
     * }
     * 
* * 返回: * *
    {@code
    
    response:[    {
            "statusCode": 200,
            "resultString": "\r\n  hao123<\/a>  About Baidu<\/a> <\/p> 

©2017 Baidu 使用百度前必读<\/a>  意见反馈<\/a> 京ICP证030173号  <\/p> <\/div> <\/div> <\/div> <\/body> <\/html>\r\n", "useTime": 1721, "headerList": [ { "name": "Cache-Control", "value": "private, no-cache, no-store, proxy-revalidate, no-transform" }, { "name": "Connection", "value": "Keep-Alive" }, { "name": "Content-Type", "value": "text/html" }, { "name": "Date", "value": "Wed, 29 Nov 2017 14:41:32 GMT" }, { "name": "Last-Modified", "value": "Mon, 23 Jan 2017 13:28:36 GMT" }, { "name": "Pragma", "value": "no-cache" }, { "name": "Server", "value": "bfe/1.0.8.18" }, { "name": "Set-Cookie", "value": "BDORZ=27315; max-age=86400; domain=.baidu.com; path=/" }, { "name": "Transfer-Encoding", "value": "chunked" } ] }] } *

* *
* * @param httpRequest * the http request * @param connectionConfig * the connection config * @return 如果 httpRequest 是null,抛出 {@link NullPointerException}
*/ public static com.feilong.net.http.HttpResponse getHttpResponse(HttpRequest httpRequest,ConnectionConfig connectionConfig){ Validate.notNull(httpRequest, "httpRequest can't be null!"); return HttpRequestExecuter.execute(httpRequest, connectionConfig, HttpResponseResultCallback.INSTANCE); } //----------------------getResponseBodyAsString----------------------------------------- /** * 发送get请求,获得请求的响应内容. * *

示例:

* *
* *

* 场景: 发信息给百度,并得到相应字符串 *

* *
     * 
     * public void testGetResponseBodyAsString(){
     *     String urlString = "http://www.baidu.com";
     * 
     *     LOGGER.debug(HttpClientUtil.get(urlString));
     * }
     * 
     * 
* * 返回: * *
    {@code
    
     百度一下,你就知道  

关于百度 About Baidu

©2017 Baidu 使用百度前必读  意见反馈 京ICP证030173号 

} *
* *
* * @param uri * 请求的uri地址 * @return 如果 uri 是null,抛出 {@link NullPointerException}
* 如果 uri 是blank,抛出 {@link IllegalArgumentException}
* @since 1.10.7 */ public static String get(String uri){ return get(uri, null); } /** * 发送 get 请求,获得请求的响应内容. * *

示例:

* *
* *

* 场景: 发信息给百度,并得到相应字符串 *

* *
     * 
     * public void testGetResponseBodyAsString(){
     *     String urlString = "http://www.baidu.com";
     * 
     *     LOGGER.debug(HttpClientUtil.get(urlString, null));
     * }
     * 
     * 
* * 返回: * *
     *     {@code
     *     
     *      百度一下,你就知道  

关于百度 About Baidu

©2017 Baidu 使用百度前必读  意见反馈 京ICP证030173号 

* * } *
* *
* * @param uri * 请求的uri地址 * @param requestParamMap * the request param map * @return 如果 uri 是null,抛出 {@link NullPointerException}
* 如果 uri 是blank,抛出 {@link IllegalArgumentException}
* @since 1.10.7 */ public static String get(String uri,Map requestParamMap){ Validate.notBlank(uri, "uri can't be blank!"); return getResponseBodyAsString(new HttpRequest(uri, requestParamMap, HttpMethodType.GET)); } //--------------------------------------------------------------- /** * 发送put请求,获得请求的响应内容. * *

示例:

* *
* *

* 场景: 发信息给百度,并得到相应字符串 *

* *
     * 
     * public void testGetResponseBodyAsString(){
     *     String urlString = "http://www.baidu.com";
     * 
     *     LOGGER.debug(HttpClientUtil.put(urlString));
     * }
     * 
     * 
* * 返回: * *
    {@code
    
     百度一下,你就知道  

关于百度 About Baidu

©2017 Baidu 使用百度前必读  意见反馈 京ICP证030173号 

} *
* *
* * @param uri * 请求的uri地址 * @return 如果 uri 是null,抛出 {@link NullPointerException}
* 如果 uri 是blank,抛出 {@link IllegalArgumentException}
* @since 1.12.5 */ public static String put(String uri){ return put(uri, null); } /** * 发送 put 请求,获得请求的响应内容. * *

示例:

* *
* *

* 场景: 发信息给百度,并得到相应字符串 *

* *
     * 
     * public void testGetResponseBodyAsString(){
     *     String urlString = "http://www.baidu.com";
     * 
     *     LOGGER.debug(HttpClientUtil.put(urlString, null));
     * }
     * 
     * 
* * 返回: * *
     *     {@code
     *     
     *      百度一下,你就知道  

关于百度 About Baidu

©2017 Baidu 使用百度前必读  意见反馈 京ICP证030173号 

* * } *
* *
* * @param uri * 请求的uri地址 * @param requestParamMap * the request param map * @return 如果 uri 是null,抛出 {@link NullPointerException}
* 如果 uri 是blank,抛出 {@link IllegalArgumentException}
* @since 1.12.5 */ public static String put(String uri,Map requestParamMap){ Validate.notBlank(uri, "uri can't be blank!"); return getResponseBodyAsString(new HttpRequest(uri, requestParamMap, HttpMethodType.PUT)); } //--------------------------------------------------------------- /** * 发送post请求,获得请求的响应内容. * *

示例:

* *
* *

* 场景: 发信息给百度,并得到相应字符串 *

* *
     * 
     * public void testGetResponseBodyAsString(){
     *     String urlString = "http://www.baidu.com";
     * 
     *     LOGGER.debug(HttpClientUtil.post(urlString));
     * }
     * 
     * 
* * 返回: * *
    {@code
    
     百度一下,你就知道  

关于百度 About Baidu

©2017 Baidu 使用百度前必读  意见反馈 京ICP证030173号 

} *
* *
* * @param uri * 请求的uri地址 * @return 如果 uri 是null,抛出 {@link NullPointerException}
* 如果 uri 是blank,抛出 {@link IllegalArgumentException}
* @since 1.10.7 */ public static String post(String uri){ return post(uri, (Map) null); } /** * 发送 post 请求,获得请求的响应内容. * *

示例:

* *
* *

* 场景: 发信息给百度,并得到相应字符串 *

* *
     * 
     * public void testGetResponseBodyAsString(){
     *     String urlString = "http://www.baidu.com";
     * 
     *     LOGGER.debug(HttpClientUtil.post(urlString, null));
     * }
     * 
     * 
* * 返回: * *
     *     {@code
     *     
     *      百度一下,你就知道  

关于百度 About Baidu

©2017 Baidu 使用百度前必读  意见反馈 京ICP证030173号 

* * } *
* *
* * @param uri * 请求的uri地址 * @param requestParamMap * the request param map * @return 如果 uri 是null,抛出 {@link NullPointerException}
* 如果 uri 是blank,抛出 {@link IllegalArgumentException}
* @since 1.10.7 */ public static String post(String uri,Map requestParamMap){ Validate.notBlank(uri, "uri can't be blank!"); return getResponseBodyAsString(new HttpRequest(uri, requestParamMap, HttpMethodType.POST)); } /** * 发送 Post 请求,并且设置 RequestBody ,获得请求的响应内容. * *

示例:

* *
* *

* 比如微信请求数据 *

* *
     * 
     * private static String getResponse(String url,Map{@code } map){
     *     String xmlInfo = XStreamUtil.toXML(map, "xml", false);
     * 
     *     return HttpClientUtil.post(url, xmlInfo);
     * }
     * 
     * 
* *
* * @param uri * the uri * @param requestBody * the request body * @return 如果 uri 是null,抛出 {@link NullPointerException}
* 如果 uri 是blank,抛出 {@link IllegalArgumentException}
* @since 1.10.7 */ public static String post(String uri,String requestBody){ return post(uri, requestBody, null); } /** * 发送 Post 请求,并且设置 RequestBody ,以及 headerMap,获得请求的响应内容. * *

示例:

* *
* *

* 比如天气预报请求数据 *

* *
     * 
     * private static String getResult(String city){
     *     String soap = buildSoapString(city);
     * 
     *     Map{@code } mapUseEntrys = toMapUseEntrys(
     *                     Pair.of("Content-Type", "text/xml; charset=utf-8"), //
     *                     Pair.of("SOAPAction", SOAP_ACTION));
     *     return HttpClientUtil.post(URL_WEATHER_SERVICE, soap, mapUseEntrys);
     * }
     * 
     * 
* *
* * @param uri * the uri * @param requestBody * the request body * @param headerMap * the header map * @return 如果 uri 是null,抛出 {@link NullPointerException}
* 如果 uri 是blank,抛出 {@link IllegalArgumentException}
* @since 2.1.0 */ public static String post(String uri,String requestBody,Map headerMap){ Validate.notBlank(uri, "uri can't be blank!"); HttpRequest httpRequest = new HttpRequest(uri, null, HttpMethodType.POST); httpRequest.setHeaderMap(headerMap); httpRequest.setRequestBody(requestBody); return getResponseBodyAsString(httpRequest); } //--------------------------------------------------------------- /** * 发送请求,获得请求的响应内容. *

示例:

* *
* *

* 场景: 发信息给百度,并得到相应字符串 *

* *
     * 
     * public void testGetResponseBodyAsString(){
     *     String urlString = "http://www.baidu.com";
     * 
     *     LOGGER.debug(HttpClientUtil.getResponseBodyAsString(urlString));
     * }
     * 
     * 
* * 返回: * *
    {@code
    
     百度一下,你就知道  

关于百度 About Baidu

©2017 Baidu 使用百度前必读  意见反馈 京ICP证030173号 

} *
* *
* * @param httpRequest * the http request * @return 如果 httpRequest 是null,抛出 {@link NullPointerException}
*/ public static String getResponseBodyAsString(HttpRequest httpRequest){ Validate.notNull(httpRequest, "httpRequest can't be null!"); return getResponseBodyAsString(httpRequest, null); } /** * 发送请求,获得请求的响应内容. * * @param uri * the uri * @param requestParamMap * the request param map * @param httpMethod * 不区分大小写, 比如get,Get,GET都可以,但是需要对应 {@link HttpMethodType}的支持的枚举值 * @return 如果 uri 是null,抛出 {@link NullPointerException}
* 如果 uri 是blank,抛出 {@link IllegalArgumentException}
* @since 1.11.0 */ public static String getResponseBodyAsString(String uri,Map requestParamMap,String httpMethod){ Validate.notBlank(uri, "uri can't be blank!"); return getResponseBodyAsString(new HttpRequest(uri, requestParamMap, httpMethod), null); } /** * 发送请求,获得请求的响应内容. * * @param uri * the uri * @param requestParamMap * the request param map * @param httpMethodType * the http method type * @return 如果 uri 是null,抛出 {@link NullPointerException}
* 如果 uri 是blank,抛出 {@link IllegalArgumentException}
* @since 1.11.0 */ public static String getResponseBodyAsString(String uri,Map requestParamMap,HttpMethodType httpMethodType){ Validate.notBlank(uri, "uri can't be blank!"); return getResponseBodyAsString(new HttpRequest(uri, requestParamMap, httpMethodType), null); } /** * 发送请求,获得请求的响应内容. * * @param uri * the uri * @param connectionConfig * the connection config * @return 如果 uri 是null,抛出 {@link NullPointerException}
* 如果 uri 是blank,抛出 {@link IllegalArgumentException}
* @since 2.1.0 */ public static String getResponseBodyAsString(String uri,ConnectionConfig connectionConfig){ Validate.notBlank(uri, "uri can't be blank!"); return getResponseBodyAsString(new HttpRequest(uri), connectionConfig); } //--------------------------------------------------------------- /** * 发送请求,获得请求的响应内容. * *

示例:

* *
* *

* 场景: 发信息给百度,并得到相应字符串 *

* *
     * 
     * public void testGetResponseBodyAsString(){
     *     String urlString = "http://www.baidu.com";
     * 
     *     LOGGER.debug(HttpClientUtil.getResponseBodyAsString(urlString));
     * }
     * 
     * 
* * 返回: * *
    {@code
    
     百度一下,你就知道  

关于百度 About Baidu

©2017 Baidu 使用百度前必读  意见反馈 京ICP证030173号 

} *
* *
* * @param httpRequest * the http request * @param connectionConfig * the connection config * @return 如果 httpRequest 是null,抛出 {@link NullPointerException}
* 如果 connectionConfig 是null,使用 {@link ConnectionConfig#INSTANCE}
*/ public static String getResponseBodyAsString(HttpRequest httpRequest,ConnectionConfig connectionConfig){ Validate.notNull(httpRequest, "httpRequest can't be null!"); return HttpRequestExecuter.execute(httpRequest, connectionConfig, ResponseBodyAsStringResultCallback.INSTANCE); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy