This repository has been archived by the owner on Jul 22, 2022. It is now read-only.
feilong net httpclient4
feilong edited this page May 22, 2020
·
29 revisions
基于Apache HttpComponents 的http 工具类
http请求,是开发中常见的功能,经常会接到需求,
发送关闭交易请求到微信 调用OMS物流接口,获得物流信息
目前项目中也有很多工具类,有以下缺点:
- 功能不够全,比如缺少代理参数设置,如果项目环境有DMZ区,那代码改动会比较大
- 异常处理过于简单
- 缺少必要的日志,不便于查询和分析问题
而 feilong-net-httpclient4
改进了这些问题
-
功能比较全,支持代理,支持超时时间设置等
-
有完备详细的log输出
-
支持常用功能,API简单易懂
feilong-net-httpclient4
jar你可以在 仓库 浏览
如果你使用 maven
, 您可以通过以下方式来配置 pom.xml
:
<project>
....
<properties>
<version.feilong-platform>2.1.0</version.feilong-platform>
....
</properties>
....
<repositories>
<repository>
<id>feilong-repository</id>
<url>https://raw.github.com/venusdrogon/feilong-platform/repository</url>
</repository>
</repositories>
....
<dependencies>
....
<dependency>
<groupId>com.feilong.net</groupId>
<artifactId>feilong-net-httpclient4</artifactId>
<version>${version.feilong-platform}</version>
</dependency>
....
</dependencies>
....
</project>
此外强烈建议你使用 feilong 工具类全家桶(含IO操作,Net操作,Json,XML,自定义标签等等工具类)
<project>
....
<properties>
<version.feilong-platform>2.1.0</version.feilong-platform>
....
</properties>
....
<repositories>
<repository>
<id>feilong-repository</id>
<url>https://raw.github.com/venusdrogon/feilong-platform/repository</url>
</repository>
</repositories>
....
<dependencies>
....
<dependency>
<groupId>com.feilong.platform</groupId>
<artifactId>feilong-util-all</artifactId>
<version>${version.feilong-platform}</version>
</dependency>
....
</dependencies>
....
</project>
该接口,提供四个方法
具体可以参见 类的 javadoc
get
public final class HttpClientUtil{
//---------------------getResponseStatusCode------------------------------------------
/**
* 获得请求的响应码.
*
* <p>
* 默认 {@link HttpMethodType#GET} 请求
* </p>
*
* <h3>示例:</h3>
*
* <blockquote>
*
* <pre class="code">
*
* public void testGetResponseBodyAsString(){
* String urlString = "http://www.baidu.com";
*
* int responseStatusCode = HttpClientUtil.getResponseStatusCode(urlString);
* LOGGER.debug("" + responseStatusCode);
* }
*
* </pre>
*
* <b>返回:</b>
*
* <pre class="code">
* 200
* </pre>
*
* </blockquote>
*
* @param urlString
* the url string
* @return 如果 <code>urlString</code> 是null,抛出 {@link NullPointerException}<br>
* 如果 <code>urlString</code> 是blank,抛出 {@link IllegalArgumentException}<br>
*/
public static int getResponseStatusCode(String urlString);
/**
* 获得请求的响应码.
*
* <p>
* 默认 {@link HttpMethodType#GET} 请求
* </p>
*
*
* <h3>示例:</h3>
*
* <blockquote>
*
* <pre class="code">
*
* public void testGetResponseBodyAsString(){
* String urlString = "http://www.baidu.com";
*
* int responseStatusCode = HttpClientUtil.getResponseStatusCode(urlString);
* LOGGER.debug("" + responseStatusCode);
* }
*
* </pre>
*
* <b>返回:</b>
*
* <pre class="code">
* 200
* </pre>
*
* </blockquote>
*
*
* @param urlString
* the url string
* @param connectionConfig
* the connection config
* @return 如果 <code>urlString</code> 是null,抛出 {@link NullPointerException}<br>
* 如果 <code>urlString</code> 是blank,抛出 {@link IllegalArgumentException}<br>
*/
public static int getResponseStatusCode(String urlString,ConnectionConfig connectionConfig);
/**
* 获得请求的响应码.
*
* <h3>示例:</h3>
*
* <blockquote>
*
* <pre class="code">
*
* public void testGetResponseBodyAsString(){
* String urlString = "http://www.baidu.com";
*
* int responseStatusCode = HttpClientUtil.getResponseStatusCode(urlString);
* LOGGER.debug("" + responseStatusCode);
* }
*
* </pre>
*
* <b>返回:</b>
*
* <pre class="code">
* 200
* </pre>
*
* </blockquote>
*
* @param httpRequest
* the http request
* @param connectionConfig
* the connection config
* @return 如果 <code>httpRequest</code> 是null,抛出 {@link NullPointerException}<br>
*/
public static int getResponseStatusCode(HttpRequest httpRequest,ConnectionConfig connectionConfig);
//---------------------------getHttpResponse------------------------------------
/**
* 获得 {@link com.feilong.net.entity.HttpResponse}.
*
* <h3>示例:</h3>
*
* <blockquote>
*
* <p>
* <b>场景:</b> 发请求到百度
* </p>
*
* <pre>
*
* public void testGetResponseBodyAsString1(){
* String urlString = "http://localhost:8081/member/login";
* urlString = "http://www.baidu.com";
*
* HttpClientUtil.getHttpResponse(urlString);
* }
* </pre>
*
* <b>返回:</b>
*
* <pre>
{@code
response:[ {
"statusCode": 200,
"resultString": "<!DOCTYPE html>\r\n<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv
=X-UA-Compatible content=
IE=Edge> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123<\/a> <a href=http://ir.baidu.com>About Baidu<\/a> <\/p> <p id=
cp>©2017 Baidu <a href=http://www.baidu.com/duty/>使用百度前必读<\/a> <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈<\/a> 京ICP证030173号 <img src=//www.baidu.com/img/gs.gif> <\/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"
}
]
}]
}
* </pre>
*
* </blockquote>
*
* @param urlString
* the url string
* @return 如果 <code>urlString</code> 是null,抛出 {@link NullPointerException}<br>
* 如果 <code>urlString</code> 是blank,抛出 {@link IllegalArgumentException}<br>
*/
public static com.feilong.net.entity.HttpResponse getHttpResponse(String urlString);
/**
* 获得 {@link com.feilong.net.entity.HttpResponse}.
*
* <h3>示例:</h3>
*
* <blockquote>
*
* <p>
* <b>场景:</b> 发请求到百度
* </p>
*
* <pre>
*
* public void testGetResponseBodyAsString1(){
* String urlString = "http://localhost:8081/member/login";
* urlString = "http://www.baidu.com";
*
* HttpClientUtil.getHttpResponse(urlString);
* }
* </pre>
*
* <b>返回:</b>
*
* <pre>
{@code
response:[ {
"statusCode": 200,
"resultString": "<!DOCTYPE html>\r\n<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv
=X-UA-Compatible content=
IE=Edge> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123<\/a> <a href=http://ir.baidu.com>About Baidu<\/a> <\/p> <p id=
cp>©2017 Baidu <a href=http://www.baidu.com/duty/>使用百度前必读<\/a> <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈<\/a> 京ICP证030173号 <img src=//www.baidu.com/img/gs.gif> <\/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"
}
]
}]
}
* </pre>
*
* </blockquote>
*
* @param urlString
* the url string
* @param connectionConfig
* the connection config
* @return 如果 <code>urlString</code> 是null,抛出 {@link NullPointerException}<br>
* 如果 <code>urlString</code> 是blank,抛出 {@link IllegalArgumentException}<br>
*/
public static com.feilong.net.entity.HttpResponse getHttpResponse(String urlString,ConnectionConfig connectionConfig);
/**
* 获得 {@link com.feilong.net.entity.HttpResponse}.
*
* <h3>示例:</h3>
*
* <blockquote>
*
* <p>
* <b>场景:</b> 发请求到百度
* </p>
*
* <pre>
*
* public void testGetResponseBodyAsString1(){
* String urlString = "http://localhost:8081/member/login";
* urlString = "http://www.baidu.com";
*
* HttpClientUtil.getHttpResponse(urlString);
* }
* </pre>
*
* <b>返回:</b>
*
* <pre>
{@code
response:[ {
"statusCode": 200,
"resultString": "<!DOCTYPE html>\r\n<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv
=X-UA-Compatible content=
IE=Edge> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123<\/a> <a href=http://ir.baidu.com>About Baidu<\/a> <\/p> <p id=
cp>©2017 Baidu <a href=http://www.baidu.com/duty/>使用百度前必读<\/a> <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈<\/a> 京ICP证030173号 <img src=//www.baidu.com/img/gs.gif> <\/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"
}
]
}]
}
* </pre>
*
* </blockquote>
*
* @param httpRequest
* the http request
* @param connectionConfig
* the connection config
* @return 如果 <code>httpRequest</code> 是null,抛出 {@link NullPointerException}<br>
*/
public static com.feilong.net.entity.HttpResponse getHttpResponse(HttpRequest httpRequest,ConnectionConfig connectionConfig);
//----------------------getResponseBodyAsString-----------------------------------------
/**
* 发送get请求,获得请求的响应内容.
*
* <h3>示例:</h3>
*
* <blockquote>
*
* <p>
* <b>场景:</b> 发信息给百度,并得到相应字符串
* </p>
*
* <pre class="code">
*
* public void testGetResponseBodyAsString(){
* String urlString = "http://www.baidu.com";
*
* LOGGER.debug(HttpClientUtil.get(urlString));
* }
*
* </pre>
*
* <b>返回:</b>
*
* <pre>
{@code
<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=
IE=Edge><meta content=always name=
referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link
=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div id=
u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id
=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=
cp>©2017 Baidu <a href=http://www.baidu.com/duty/>使用百度前必读</a> <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a> 京ICP证030173号 <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>
}
* </pre>
*
* </blockquote>
*
* @param uri
* 请求的uri地址
* @return 如果 <code>uri</code> 是null,抛出 {@link NullPointerException}<br>
* 如果 <code>uri</code> 是blank,抛出 {@link IllegalArgumentException}<br>
* @since 1.10.7
*/
public static String get(String uri);
/**
* 发送post请求,获得请求的响应内容.
*
* <h3>示例:</h3>
*
* <blockquote>
*
* <p>
* <b>场景:</b> 发信息给百度,并得到相应字符串
* </p>
*
* <pre class="code">
*
* public void testGetResponseBodyAsString(){
* String urlString = "http://www.baidu.com";
*
* LOGGER.debug(HttpClientUtil.post(urlString));
* }
*
* </pre>
*
* <b>返回:</b>
*
* <pre>
{@code
<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=
IE=Edge><meta content=always name=
referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link
=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div id=
u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id
=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=
cp>©2017 Baidu <a href=http://www.baidu.com/duty/>使用百度前必读</a> <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a> 京ICP证030173号 <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>
}
* </pre>
*
* </blockquote>
*
* @param uri
* 请求的uri地址
* @return 如果 <code>uri</code> 是null,抛出 {@link NullPointerException}<br>
* 如果 <code>uri</code> 是blank,抛出 {@link IllegalArgumentException}<br>
* @since 1.10.7
*/
public static String post(String uri);
/**
* 发送 get 请求,获得请求的响应内容.
*
* <h3>示例:</h3>
*
* <blockquote>
*
* <p>
* <b>场景:</b> 发信息给百度,并得到相应字符串
* </p>
*
* <pre class="code">
*
* public void testGetResponseBodyAsString(){
* String urlString = "http://www.baidu.com";
*
* LOGGER.debug(HttpClientUtil.get(urlString, null));
* }
*
* </pre>
*
* <b>返回:</b>
*
* <pre>
* {@code
* <!DOCTYPE html>
* <!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=
X-UA-Compatible content=
* IE=Edge><meta content=always name=
* referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link
* =#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div id=
* u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id
* =ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=
* cp>©2017 Baidu <a href=http://www.baidu.com/duty/>使用百度前必读</a> <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a> 京ICP证030173号 <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>
*
* }
* </pre>
*
* </blockquote>
*
* @param uri
* 请求的uri地址
* @param requestParamMap
* the request param map
* @return 如果 <code>uri</code> 是null,抛出 {@link NullPointerException}<br>
* 如果 <code>uri</code> 是blank,抛出 {@link IllegalArgumentException}<br>
* @since 1.10.7
*/
public static String get(String uri,Map<String, String> requestParamMap);
/**
* 发送 post 请求,获得请求的响应内容.
*
* <h3>示例:</h3>
*
* <blockquote>
*
* <p>
* <b>场景:</b> 发信息给百度,并得到相应字符串
* </p>
*
* <pre class="code">
*
* public void testGetResponseBodyAsString(){
* String urlString = "http://www.baidu.com";
*
* LOGGER.debug(HttpClientUtil.post(urlString, null));
* }
*
* </pre>
*
* <b>返回:</b>
*
* <pre>
* {@code
* <!DOCTYPE html>
* <!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=
X-UA-Compatible content=
* IE=Edge><meta content=always name=
* referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link
* =#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div id=
* u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id
* =ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=
* cp>©2017 Baidu <a href=http://www.baidu.com/duty/>使用百度前必读</a> <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a> 京ICP证030173号 <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>
*
* }
* </pre>
*
* </blockquote>
*
* @param uri
* 请求的uri地址
* @param requestParamMap
* the request param map
* @return 如果 <code>uri</code> 是null,抛出 {@link NullPointerException}<br>
* 如果 <code>uri</code> 是blank,抛出 {@link IllegalArgumentException}<br>
* @since 1.10.7
*/
public static String post(String uri,Map<String, String> requestParamMap);
/**
* 发送 Post 请求,并且设置 RequestBody ,获得请求的响应内容.
*
* <h3>示例:</h3>
*
* <blockquote>
*
* <p>
* 比如微信请求数据
* </p>
*
* <pre class="code">
*
* private static String getResponse(String url,Map{@code <String, String>} map){
* String xmlInfo = XStreamUtil.toXML(map, "xml", false);
*
* return HttpClientUtil.post(url, xmlInfo);
* }
*
* </pre>
*
* </blockquote>
*
* @param uri
* the uri
* @param requestBody
* the request body
* @return the string
*
* @since 1.10.7
*/
public static String post(String uri,String requestBody);
/**
* 发送请求,获得请求的响应内容.
* <h3>示例:</h3>
*
* <blockquote>
*
* <p>
* <b>场景:</b> 发信息给百度,并得到相应字符串
* </p>
*
* <pre class="code">
*
* public void testGetResponseBodyAsString(){
* String urlString = "http://www.baidu.com";
*
* LOGGER.debug(HttpClientUtil.getResponseBodyAsString(urlString));
* }
*
* </pre>
*
* <b>返回:</b>
*
* <pre>
{@code
<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=
IE=Edge><meta content=always name=
referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link
=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div id=
u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id
=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=
cp>©2017 Baidu <a href=http://www.baidu.com/duty/>使用百度前必读</a> <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a> 京ICP证030173号 <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>
}
* </pre>
*
* </blockquote>
*
* @param httpRequest
* the http request
* @return the response body as string
*/
public static String getResponseBodyAsString(HttpRequest httpRequest);
/**
* 发送请求,获得请求的响应内容.
*
* <h3>示例:</h3>
*
* <blockquote>
*
* <p>
* <b>场景:</b> 发信息给百度,并得到相应字符串
* </p>
*
* <pre class="code">
*
* public void testGetResponseBodyAsString(){
* String urlString = "http://www.baidu.com";
*
* LOGGER.debug(HttpClientUtil.getResponseBodyAsString(urlString));
* }
*
* </pre>
*
* <b>返回:</b>
*
* <pre>
{@code
<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=
IE=Edge><meta content=always name=
referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link
=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div id=
u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id
=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=
cp>©2017 Baidu <a href=http://www.baidu.com/duty/>使用百度前必读</a> <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a> 京ICP证030173号 <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>
}
* </pre>
*
* </blockquote>
*
* @param httpRequest
* the http request
* @param connectionConfig
* the connection config
* @return 如果 <code>httpRequest</code> 是null,抛出 {@link NullPointerException}<br>
* 如果 <code>connectionConfig</code> 是null,使用 {@link ConnectionConfig#INSTANCE}<br>
*/
public static String getResponseBodyAsString(HttpRequest httpRequest,ConnectionConfig connectionConfig);
QQ 群 243306798
|
---|
如果您对本项目有任何建议和批评,可以使用下面的联系方式:
- iteye博客:http://feitianbenyue.iteye.com/