一、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. config-server-bus动态更新配置

    config-server用来搭建配置中心,而配置信息一般使用gitlab仓库来存储,这样在你的配置发生改变时,不需要从新打包,而如果使用native的试,则需要从新打一个config-server的 ...

  2. Kotlin exception

    cannot generate view binders java.lang.StackOverflowError 最近写kotlin项目,使用databinding,在适配器中定义了事件接口,在适配 ...

  3. Oracle 11g与12c的审计详解

    最近遇到一些脚本诱发的审计相关BUG,感觉有必要重新梳理一下11g与12c的审计模式,于是根据官网修正了一下以前的一篇笔记这里发出来. 一.审计功能的开启: SQL> show paramete ...

  4. RMAN异机恢复主要步骤和注意事项

    以后改行了或老了回头看看,我曾经会这些,也是件愉快的事 [备份]--创建目录[oracle@test20 backup]$ mkdir -p /home/oracle/backup--备份脚本[ora ...

  5. C#&.Net干货分享- 构建PrinterHelper直接调用打印机相关操作

    namespace Frame.Printer{    /// <summary>    ///     /// </summary>    public class Prin ...

  6. Unity3D VidoePlayer 加载StreamingAssets下视频

    using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.UI;usi ...

  7. pytest系列(二):筛选用例新姿势,mark 一下,你就知道。

    pytest系列(一)中给大家介绍了pytest的特性,以及它的编写用例的简单至极. 那么在实际工作当中呢,我们要写的自动化用例会比较多,不会都放在一个py文件里. 如下图所示,我们编写的用例存放在不 ...

  8. C++继承产生的问题

    今天写代码,用到了继承,忘了将父类中的私有成员改为protected,结果一调用父类地函数后,子类中的root指针直接变为了父类中的空的root.私有成员在继承后依然会保留,占一定的内存空间,但却没有 ...

  9. jquery使用on()方法绑定的事件被执行多次的问题

    jQuery用on()方法绑定了事件之后,在代码执行过程中,可能会遇到事件被多次执行的情况. 本来以为是事件冒泡的问题,后来发现是on()方法的特性引起的问题. 简单还原一下问题的场景 这里简单还原一 ...

  10. PAT 1011 World Cup Betting 查找元素

    With the 2010 FIFA World Cup running, football fans the world over were becoming increasingly excite ...