HttpClient基本功能的使用 Get方式
一、GET 方法
使用 HttpClient 需要以下 6 个步骤:
1. 创建 HttpClient 的实例
2. 创建某种连接方法的实例,在这里是 GetMethod。在 GetMethod 的构造函数中传入待连接的地址
3. 调用第一步中创建好的实例的 execute 方法来执行第二步中创建好的 method 实例
4. 读 response
5. 释放连接。无论执行方法是否成功,都必须释放连接
6. 对得到后的内容进行处理
根据以上步骤,我们来编写用GET方法取得某网页内容的代码。
1、大部分情况下 HttpClient 默认的构造函数已经足够使用。
HttpClient httpClient = new HttpClient();
2、创建GET方法的实例。
在GET方法的构造函数中传入待连接的地址即可。用GetMethod将会自动处理转发过程,如果想要把自动处理转发过程去掉的话,可以调用方法setFollowRedirects(false)。
GetMethod getMethod = new GetMethod( " http://www.ibm.com/ " );
3、调用 httpClient 的 executeMethod 方法来执行 getMethod。
由于是执行在网络上的程序,在运行executeMethod方法的时候,需要处理两个异常,分别是HttpException和 IOException。
HttpException:引起第一种异常的原因主要可能是在构造getMethod的时候传入的协议不对,比如将"http"写成了"htp",或者服务 器端返回的内容不正常等,并且该异常发生是不可恢复的;
IOException:一般是由于网络原因引起的异常,对于这种异常 (IOException),HttpClient会根据你指定的恢复策略自动试着重新执行executeMethod方法。HttpClient的恢复 策略可以自定义(通过实现接口HttpMethodRetryHandler来实现)。通过httpClient的方法setParameter设置你实 现的恢复策略。
本例中使用的是系统提供的默认恢复策略,该策略在碰到第二类异常的时候将自动重试3次。executeMethod返回值是一个整数,表示 了执行该方法后服务器返回的状态码,该状态码能表示出该方法执行是否成功、需要认证或 者页面发生了跳转(默认状态下GetMethod的实例是自动处理跳 转的)等。
getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,new DefaultHttpMethodRetryHandler()); // 设置成了默认的恢复策略,在发生异常时候将自动重试3次,在这里你也可以设置成自定义的恢复策略
int statusCode = client.executeMethod(getMethod); // 执行getMethod
if (statusCode != HttpStatus.SC_OK) {
System.err.println( " Method failed: " + getMethod.getStatusLine());
}
4、在返回的状态码正确后,即可取得内容。
取得目标地址的内容有三种方法:
Ⅰ、getResponseBody,该方法返回的是目标的二进制的byte流;
Ⅱ、getResponseBodyAsString,这个方法返回的是String类型,值得注意的是该方法返回的String的编码是根据系统默认的编码方式,所以返回的String值可能编码类型有误;
Ⅲ、getResponseBodyAsStream,这个方法对于目标地址中有大量数据需要传输是最佳的。
在这里我们使用了最简单的 getResponseBody方法。
byte [] responseBody = method.getResponseBody();
5、释放连接。
无论执行方法是否成功,都必须释放连接。
method.releaseConnection();
6、处理内容。
在这一步中根据你的需要处理内容,本例中只是简单的将内容打印到控制台。System.out.println( new String(responseBody));
package com.asiainfo.hsop.common.utils;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpClientParams;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.httpclient.protocol.Protocol;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; /**
* 处理http和https协议请求,根据请求url判断协议类型
*
* @author yangxiaobing
* @date 2017/9/6
*/
public class HttpUtil { private static Logger logger = LogManager.getLogger(HttpUtil.class);
private static final int HTTP_DEFAULT_TIMEOUT = 15000; //超时时间默认为15秒
private static final int HTTP_SOCKET_TIMEOUT = 30000; //连接状态下没有收到数据的话强制断时间为30秒
private static final int MAX_TOTAL_CONNECTIONS = 500; //最大连接数
private static final int CONN_MANAGER_TIMEOUT = 500; //该值就是连接不够用的时候等待超时时间,一定要设置,而且不能太大 private static MultiThreadedHttpConnectionManager httpConnectionManager = new MultiThreadedHttpConnectionManager(); //接口调用频繁,结果出现了很多ConnectTimeoutException 配置优化,共用HttpClient,减少开销
static {
//每主机最大连接数和总共最大连接数,通过hosfConfiguration设置host来区分每个主机
//client.getHttpConnectionManager().getParams().setDefaultMaxConnectionsPerHost(8);
httpConnectionManager.getParams().setMaxTotalConnections(MAX_TOTAL_CONNECTIONS);
httpConnectionManager.getParams().setConnectionTimeout(HTTP_DEFAULT_TIMEOUT);//连接超时时间
httpConnectionManager.getParams().setSoTimeout(HTTP_SOCKET_TIMEOUT); //连接状态下没有收到数据的话强制断时间
httpConnectionManager.getParams().setLongParameter(HttpClientParams.CONNECTION_MANAGER_TIMEOUT, CONN_MANAGER_TIMEOUT);
//是否计算节省带宽
httpConnectionManager.getParams().setTcpNoDelay(true);
//延迟关闭时间
httpConnectionManager.getParams().setLinger(0);
//失败的情况下会默认进行3次尝试,成功之后不会再尝试 ------关闭
httpConnectionManager.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(0, false));
} /**
* 构建 httpsclient 请求
*
* @return
*/
private static HttpClient getHttpClient() {
HttpClient httpClient = new HttpClient(httpConnectionManager);
httpClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
return httpClient;
} /**
* POST请求统一入口
*/
public static String post(String url, Map<String, Object> paramMap) {
logger.info("===>>>调用[post]接口开始...===>>> URL:" + url);
Long beginTime = System.currentTimeMillis();
if (StringUtils.isEmpty(url)) {
return null;
}
String result = null;
if (startsWithIgnoreCase(url, "https")) {
result = httpsPost(url, paramMap, "UTF-8");
} else if (startsWithIgnoreCase(url, "http")) {
result = httpPost(url, paramMap, "UTF-8");
} else {
logger.warn("http url format error!");
}
logger.info("===>>>调用[post]接口结束 ===>>>URL:" + url + ",耗时:" + (System.currentTimeMillis() - beginTime) + "毫秒");
return result;
} /**
* GET请求统一入口
*/
public static String get(String url) {
logger.info("===>>>调用[get]接口开始...===>>> URL:" + url);
Long beginTime = System.currentTimeMillis();
if (StringUtils.isEmpty(url)) {
return null;
}
String result = null;
if (startsWithIgnoreCase(url, "https")) {
result = httpsGet(url, "UTF-8");
} else if (startsWithIgnoreCase(url, "http")) {
result = httpGet(url, "UTF-8");
} else {
logger.warn("http url format error!");
}
logger.info("===>>>调用[get]接口结束 ===>>>URL:" + url + ",耗时:" + (System.currentTimeMillis() - beginTime) + "毫秒");
return result;
} public static String httpPost(String url, Map<String, Object> paramMap, String encoding) {
String content = null;
if (paramMap == null) {
paramMap = new HashMap<String, Object>();
}
logger.info("http param:" + paramMap.toString());
HttpClient httpClient = getHttpClient();
PostMethod method = new PostMethod(url); if (!paramMap.isEmpty()) {
for (Map.Entry<String, ?> entry : paramMap.entrySet()) {
method.addParameter(new NameValuePair(entry.getKey(), entry.getValue().toString()));
}
}
try {
httpClient.executeMethod(method);
logger.info("http status : " + method.getStatusLine().getStatusCode());
content = new String(method.getResponseBody(), encoding);
logger.info("http response : [" + content + "]");
} catch (Exception e) {
logger.error("发起http请求失败[" + url + "]" + ",param" + paramMap.toString(), e);
} finally {
method.releaseConnection();
httpClient.getHttpConnectionManager().closeIdleConnections(0);
}
return content;
} public static String httpPost(String url, JSONObject param) {
String content = null;
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url); httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
StringEntity se = null;
try {
se = new StringEntity(JSONObject.toJSONString(param));
se.setContentType("text/json");
httpPost.setEntity(se); CloseableHttpResponse response = null;
response = httpClient.execute(httpPost);
HttpEntity httpEntity = response.getEntity();
content = EntityUtils.toString(httpEntity, "UTF-8");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (httpClient != null) {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return content;
} private static String httpsPost(String url, Map<String, Object> paramMap, String encoding) {
String content = null;
HttpClient httpsClient = getHttpClient();
Protocol myhttps = new Protocol("https", new MySSLProtocolSocketFactory(), 443);
Protocol.registerProtocol("https", myhttps);
PostMethod method = new PostMethod(url);
if (paramMap != null && !paramMap.isEmpty()) {
for (Map.Entry<String, Object> entry : paramMap.entrySet()) {
if (null != entry.getValue()) {
method.addParameter(new NameValuePair(entry.getKey(), entry.getValue().toString()));
}
}
logger.info("https param : " + paramMap.toString());
}
try {
httpsClient.executeMethod(method);
logger.info("https status :" + method.getStatusLine().getStatusCode());
if (method.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
content = new String(method.getResponseBody(), encoding);
logger.info("https response : [" + content + "]");
}
} catch (Exception e) {
logger.error("https request failed. url : [" + url + "]" + ", param : [" + paramMap + "]", e);
} finally {
method.releaseConnection();
httpsClient.getHttpConnectionManager().closeIdleConnections(0);
}
return content;
} private static String httpGet(String url, String encoding) {
HttpClient httpClient = getHttpClient();
GetMethod method = new GetMethod(url);
String result = null;
try {
httpClient.executeMethod(method);
int status = method.getStatusCode();
if (status == HttpStatus.SC_OK) {
result = method.getResponseBodyAsString();
} else {
logger.error("Method failed: " + method.getStatusLine());
}
} catch (HttpException e) {
// 发生致命的异常,可能是协议不对或者返回的内容有问题
logger.error("Please check your provided http address!");
logger.error(e, e);
} catch (IOException e) {
// 发生网络异常
logger.error("发生网络异常!");
logger.error(e, e);
} finally {
// 释放连接
method.releaseConnection();
httpClient.getHttpConnectionManager().closeIdleConnections(0);
}
return result;
} private static String httpsGet(String url, String encoding) {
HttpClient httpsClient = getHttpClient();//HttpConnectionManager.alwaysClose=true
Protocol myhttps = new Protocol("https", new MySSLProtocolSocketFactory(), 443);
Protocol.registerProtocol("https", myhttps);
GetMethod method = new GetMethod(url);
String content = null;
try {
httpsClient.executeMethod(method);
logger.info("https status : " + method.getStatusLine().getStatusCode());
if (method.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
content = new String(method.getResponseBody(), encoding);
logger.info("https response : [" + content + "]");
}
} catch (Exception e) {
logger.error("https request failed. url : [" + url + "]", e.getCause());
} finally {
method.releaseConnection();
httpsClient.getHttpConnectionManager().closeIdleConnections(0);
}
return content;
} private static boolean startsWithIgnoreCase(String origin, String prefix) {
int len = prefix.length();
if (len == 0 || origin.length() < len) {
return false;
}
while (len-- > 0) {
char a = origin.charAt(len);
char b = prefix.charAt(len);
if (a == b || Character.toUpperCase(a) == Character.toUpperCase(b)) {
continue;
}
return false;
}
return true;
} public static void main(String args[]) { // String url = "http://10.253.6.202:30001/dipic/api/auditControl/queryTraffic";
String url = "http://localhost:8180/comm/api/upload.do";
JSONObject Json = new JSONObject();
File file = new File("E:\\initParam.properties");
Map<String,String> map = new HashMap<>();
map.put("fileName","redis-64bit.rar");
System.out.println(HttpUtil.postFile(url, file));
} //文件上传,调用fastDFS方法封装层
public static String postFile(String url, File file){
CloseableHttpClient httpclient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String result = null;
try {
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(3000)
.setConnectTimeout(3000).setConnectionRequestTimeout(3000).build();
// if (params != null && !params.isEmpty()) {
// List<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>(params.size());
// for (Map.Entry<String, String> entry : params.entrySet()) {
// String value = entry.getValue();
// if (value != null) {
// pairs.add(new BasicNameValuePair(entry.getKey(), value));
// }
// }
// url += "?" + EntityUtils.toString(new UrlEncodedFormEntity(pairs, charset));
// }
HttpPost httpPost = new HttpPost(url);
httpPost.setConfig(requestConfig);
// 将java.io.File对象添加到HttpEntity(org.apache.http.HttpEntity)对象中
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
// 解决上传文件,文件名中文乱码问题
builder.setCharset(Charset.forName("utf-8"));
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);// 设置浏览器兼容模式
builder.addPart("file", new FileBody(file)); httpPost.setEntity(builder.build());
response = httpclient.execute(httpPost); int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
HttpEntity resEntity = response.getEntity();
result = EntityUtils.toString(resEntity); // 消耗掉response
EntityUtils.consume(resEntity);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
if(response!=null){
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(httpclient!=null){
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
} }
return result;
} }
HttpUtil工具类
package test;
import java.io.IOException;
import org.apache.commons.httpclient. * ;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
public class GetSample{
public static void main(String[] args) {
// 构造HttpClient的实例
HttpClient httpClient = new HttpClient();
// 创建GET方法的实例
GetMethod getMethod = new GetMethod( " http://www.ibm.com " );
// 使用系统提供的默认的恢复策略
getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler());
try {
// 执行getMethod
int statusCode = httpClient.executeMethod(getMethod);
if (statusCode != HttpStatus.SC_OK) {
System.err.println( " Method failed: "
+ getMethod.getStatusLine());
}
// 读取内容
byte [] responseBody = getMethod.getResponseBody();
// 处理内容
System.out.println( new String(responseBody));
} catch (HttpException e) {
// 发生致命的异常,可能是协议不对或者返回的内容有问题
System.out.println( " Please check your provided http address! " );
e.printStackTrace();
} catch (IOException e) {
// 发生网络异常
e.printStackTrace();
} finally {
// 释放连接
getMethod.releaseConnection();
}
}
}
HttpClient基本功能的使用 Get方式的更多相关文章
- C# 中一些类关系的判定方法 C#中关于增强类功能的几种方式 Asp.Net Core 轻松学-多线程之取消令牌
1. IsAssignableFrom实例方法 判断一个类或者接口是否继承自另一个指定的类或者接口. public interface IAnimal { } public interface ID ...
- C#中关于增强类功能的几种方式
C#中关于增强类功能的几种方式 本文主要讲解如何利用C#语言自身的特性来对一个类的功能进行丰富与增强,便于拓展现有项目的一些功能. 拓展方法 扩展方法被定义为静态方法,通过实例方法语法进行调用.方法的 ...
- HttpClient获取Cookie的两种方式
转载:http://blog.csdn.net/zhangbinu/article/details/72777620 一.旧版本的HttpClient获取Cookies p.s. 该方式官方已不推荐使 ...
- Java基础知识强化之网络编程笔记19:Android网络通信之 HttpClient和传统Post、Get方式的区别
1. HttpClient是什么 ? HTTP 协议可能是现在 Internet 上使用得最多.最重要的协议了,越来越多的 Java 应用程序需要直接通过 HTTP 协议来访问网络资源.虽然在 ...
- 短视频 SDK 功能点技术实现方式详解
第三方短视频解决方案作为快速切入短视频行业的首选方式,选择一款功能齐全.性能优异的短视频解决方案十分重要. 今天我们来谈谈短视频 SDK 6大重要功能点及其技术实现方式. 短视频拍摄 断点续拍 指在拍 ...
- HttpClient 传输文件的两种方式
1. org.apache.commons.httpclient.HttpClient 1.1 pom <dependency> <groupId>org.apache.htt ...
- @Autowired注解和启动自动扫描的三种方式(spring bean配置自动扫描功能的三种方式)
前言: @Autowired注解代码定义 @Target({ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.METHOD, Elemen ...
- 史上最全面的SignalR系列教程-3、SignalR 实现推送功能-集线器类实现方式
1.概述 通过前两篇 史上最全面的SignalR系列教程-1.认识SignalR 史上最全面的SignalR系列教程-2.SignalR 实现推送功能-永久连接类实现方式 文章对SignalR的介绍, ...
- 总结Ajax验证注册功能的两种方式
方法一:使用jqueryForm插件提交表单注册 ①首先引入jquery和jqueryForm插件 <script type="text/javascript" src=&q ...
随机推荐
- Redis缓存NoSQL
下面是一些关于Redis比较好的文章,因为篇幅较大,我就将其折叠起来了.不太喜欢分不同的笔记去记载,除非真的很多很多.所以本文不仅要对Redis做简单的介绍,还要分别介绍Redis中的五种结构,并会贴 ...
- Pluralsight 科技公司公布自己的avaScript 成为最受欢迎的开发技术
根据 SDTimes 报道,Pluralsight 科技公司公布自己的 Technology Index,JavaScript 位居榜首. Pluralsight,是美国的一家面向软件开发者的在线教育 ...
- 8 个 Tips 让你更好的进行 Code Review
摘要: Code Review 可以提高代码质量. 原文:Elevenbeans 作者:前端小智 Fundebug经授权转载,版权归原作者所有. 原文地址:https://kellysutton.co ...
- find 常用命令
系统中总会不断产生一些文件,比如日志文件,不一定会用到也不会自动删除,这时候就需要手动删除,当然也可以转存到其他目录下.不好找的时候可以用find模糊查找,加个job定时任务自动执行定期删除文件1.添 ...
- 并发编程~~~协程~~~greenlet模块, gevent模块
一 协程 1. 协程: 单线程下的并发,又称微线程,纤程.协程是一种用户态的轻量级线程,即协程是由用户程序自己控制调度的. 并发真正的核心: 切换并且保持状态. 开启协程并发的执行,自己的程序把控着C ...
- Linux系统学习 十四、VSFTP服务—配置文件解析、客户端使用
3.配置文件解析 默认配置选项: 一般情况下不允许匿名用户登录 全局配置选项:(手工添加) listen_address=192.168.4.1 #设置监听地址 listen_ ...
- 项目如何部署在linux系统上
前面已经安装好centos的系统,网络配置,以及部署的环境已成功啦... 下面记录的是如何部署一个项目 四个步骤: (1)放war包 (2)执行数据库脚本 (3)修改数据库的配置文件 (4)重启tom ...
- 09. Go 语言并发
Go 语言并发 并发指在同一时间内可以执行多个任务.并发编程含义比较广泛,包含多线程编程.多进程编程及分布式程序等.本章讲解的并发含义属于多线程编程. Go 语言通过编译器运行时(runtime),从 ...
- java之集合(Set、List、Map)
java集合类存放于java,uti包中,是一个用于存放对象的容器. 集合只能存放对象,比如存入的是int型数据1,那么它会自动转换成Integer包装类后再存入: 集合存放的是多个对象的引用,对象本 ...
- IT兄弟连 HTML5教程 HTML5文字版面和编辑标签 HTML框架结构
使用HTML框架结构可以把一个浏览器窗口划分为若干个小窗口,每个窗口可以显示不同的URL网页,每个框架里的网页相互独立.这样不仅可以非常方便地在浏览器中同时浏览不同的页面效果,而且可以非常方便地完成导 ...