一、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方式的更多相关文章

  1. C# 中一些类关系的判定方法 C#中关于增强类功能的几种方式 Asp.Net Core 轻松学-多线程之取消令牌

    1.  IsAssignableFrom实例方法 判断一个类或者接口是否继承自另一个指定的类或者接口. public interface IAnimal { } public interface ID ...

  2. C#中关于增强类功能的几种方式

    C#中关于增强类功能的几种方式 本文主要讲解如何利用C#语言自身的特性来对一个类的功能进行丰富与增强,便于拓展现有项目的一些功能. 拓展方法 扩展方法被定义为静态方法,通过实例方法语法进行调用.方法的 ...

  3. HttpClient获取Cookie的两种方式

    转载:http://blog.csdn.net/zhangbinu/article/details/72777620 一.旧版本的HttpClient获取Cookies p.s. 该方式官方已不推荐使 ...

  4. Java基础知识强化之网络编程笔记19:Android网络通信之 HttpClient和传统Post、Get方式的区别

    1. HttpClient是什么 ?     HTTP 协议可能是现在 Internet 上使用得最多.最重要的协议了,越来越多的 Java 应用程序需要直接通过 HTTP 协议来访问网络资源.虽然在 ...

  5. 短视频 SDK 功能点技术实现方式详解

    第三方短视频解决方案作为快速切入短视频行业的首选方式,选择一款功能齐全.性能优异的短视频解决方案十分重要. 今天我们来谈谈短视频 SDK 6大重要功能点及其技术实现方式. 短视频拍摄 断点续拍 指在拍 ...

  6. HttpClient 传输文件的两种方式

    1. org.apache.commons.httpclient.HttpClient 1.1 pom <dependency> <groupId>org.apache.htt ...

  7. @Autowired注解和启动自动扫描的三种方式(spring bean配置自动扫描功能的三种方式)

    前言: @Autowired注解代码定义 @Target({ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.METHOD, Elemen ...

  8. 史上最全面的SignalR系列教程-3、SignalR 实现推送功能-集线器类实现方式

    1.概述 通过前两篇 史上最全面的SignalR系列教程-1.认识SignalR 史上最全面的SignalR系列教程-2.SignalR 实现推送功能-永久连接类实现方式 文章对SignalR的介绍, ...

  9. 总结Ajax验证注册功能的两种方式

    方法一:使用jqueryForm插件提交表单注册 ①首先引入jquery和jqueryForm插件 <script type="text/javascript" src=&q ...

随机推荐

  1. 如何利用随机数产生验证码(java基础知识)

    以前我们通用的验证码都是五个不同的大小写字母,那么今天我就带大家学习一下利用Java基础怎么生成验证码.首先我们应该有一个清晰的思路:首先定义一个固定长度的数组用来存储需要生成的字母:其次生成随机数, ...

  2. double小数位数的显示

    不显示小数点后的0,只显示2位小数 DecimalFormat df = new DecimalFormat(".##"); double num = 450.029000089; ...

  3. Android RecyclerView SearchView基本用法1

    版权声明:本文为xing_star原创文章,转载请注明出处! 本文同步自http://javaexception.com/archives/82 背景: 做了很多年的app开发,貌似没见过没有搜索功能 ...

  4. cordova+vue 项目打包成APK应用遇到的问题和解决方法

    公司前端界面用的是vue,我要嵌入到Android中生成App第一步:安装nodenode安装:直接进入官网https://nodejs.org/zh-cn/,下载最新版本安装.安装之后在命令行中使用 ...

  5. 升级python2.7至python3.7

    最近在centos7下执行命令时,出现以下提示: DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020 ...

  6. Cocos2d-x项目编译为Android应用——命令行方式

    配置: 相关工具:Cocos2d-x 3.2 + Ant 1.9.4 + Android NDK r9d + Android SDK 运行平台:OS X 10.9.4+ Xcode 6 前言:笔者使用 ...

  7. Python的map方法的应用

    Map方法,第一个参数要写一个匿名函数表达式,或者是一个函数引用,第二个第三个往后都是表达式用到的参数,参数一般是可迭代的 1.比如下面这个map方法,两个参数,第一个 lambda x: x*x是匿 ...

  8. excel提取一类具有相似结构的部分数据,2种方式;数据——分列——分割符号/固定宽度;

    1.数据如同下图,这里我们需要提取 ¥...¥,也就是2¥及其中的内容: 鼠标选种某条数据,然后按Ctrl+A,则选种需要的所有数据:点击数据——分列:  2.根据分割符号分列:  3.选择合适的分隔 ...

  9. Mybatis----resultMap类型详解

    Mybatis----resultMap类型详解 这篇文章主要给大家介绍了关于Mybatis中强大的resultMap功能的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Mybatis具 ...

  10. 【SQL】多表查询中的 外连接 ,on,where

    先简单粗暴给个结论,多表连结查询中,on比where更早起作用,系统首先根据各个表之间的联接条件,把多个表合成一个临时表后,再由where进行匹配过滤,where后语句为真,则能查询出来,而通过外连接 ...