除了本文举荐的几种形式,强烈推荐 OkHttp
目前 JAVA 实现 HTTP 申请的办法用的最多的有两种:一种是通过 HTTPClient 这种第三方的开源框架去实现。HTTPClient 对 HTTP 的封装性比拟不错,通过它基本上可能满足咱们大部分的需要。
HttpClient3.1 是 org.apache.commons.httpclient 下操作近程 url 的工具包,尽管已不再更新,但实现工作中应用 httpClient3.1 的代码还是很多,HttpClient4.5 是 org.apache.http.client 下操作近程 url 的工具包,最新的;
另一种则是通过 HttpURLConnection 去实现,HttpURLConnection 是 JAVA 的规范类,是 JAVA 比拟原生的一种实现形式。
本人在工作中三种形式都用到过,总结一下分享给大家,也不便本人当前应用,话不多说上代码。
第一种形式:java 原生 HttpURLConnection
package com.powerX.httpClient;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class HttpClient {public static String doGet(String httpurl) {
HttpURLConnection connection = null;
InputStream is = null;
BufferedReader br = null;
String result = null;// 返回后果字符串
try {
// 创立近程 url 连贯对象
URL url = new URL(httpurl);
// 通过近程 url 连贯对象关上一个连贯,强转成 httpURLConnection 类
connection = (HttpURLConnection) url.openConnection();
// 设置连贯形式:get
connection.setRequestMethod("GET");
// 设置连贯主机服务器的超时工夫:15000 毫秒
connection.setConnectTimeout(15000);
// 设置读取近程返回的数据工夫:60000 毫秒
connection.setReadTimeout(60000);
// 发送申请
connection.connect();
// 通过 connection 连贯,获取输出流
if (connection.getResponseCode() == 200) {is = connection.getInputStream();
// 封装输出流 is,并指定字符集
br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
// 存放数据
StringBuffer sbf = new StringBuffer();
String temp = null;
while ((temp = br.readLine()) != null) {sbf.append(temp);
sbf.append("\r\n");
}
result = sbf.toString();}
} catch (MalformedURLException e) {e.printStackTrace();
} catch (IOException e) {e.printStackTrace();
} finally {
// 敞开资源
if (null != br) {
try {br.close();
} catch (IOException e) {e.printStackTrace();
}
}
if (null != is) {
try {is.close();
} catch (IOException e) {e.printStackTrace();
}
}
connection.disconnect();// 敞开近程连贯}
return result;
}
public static String doPost(String httpUrl, String param) {
HttpURLConnection connection = null;
InputStream is = null;
OutputStream os = null;
BufferedReader br = null;
String result = null;
try {URL url = new URL(httpUrl);
// 通过近程 url 连贯对象关上连贯
connection = (HttpURLConnection) url.openConnection();
// 设置连贯申请形式
connection.setRequestMethod("POST");
// 设置连贯主机服务器超时工夫:15000 毫秒
connection.setConnectTimeout(15000);
// 设置读取主机服务器返回数据超时工夫:60000 毫秒
connection.setReadTimeout(60000);
// 默认值为:false,当向近程服务器传送数据 / 写数据时,须要设置为 true
connection.setDoOutput(true);
// 默认值为:true,以后向近程服务读取数据时,设置为 true,该参数可有可无
connection.setDoInput(true);
// 设置传入参数的格局: 申请参数应该是 name1=value1&name2=value2 的模式。connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
// 设置鉴权信息:Authorization: Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0
connection.setRequestProperty("Authorization", "Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0");
// 通过连贯对象获取一个输入流
os = connection.getOutputStream();
// 通过输入流对象将参数写出去 / 传输进来, 它是通过字节数组写出的
os.write(param.getBytes());
// 通过连贯对象获取一个输出流,向近程读取
if (connection.getResponseCode() == 200) {is = connection.getInputStream();
// 对输出流对象进行包装:charset 依据工作项目组的要求来设置
br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
StringBuffer sbf = new StringBuffer();
String temp = null;
// 循环遍历一行一行读取数据
while ((temp = br.readLine()) != null) {sbf.append(temp);
sbf.append("\r\n");
}
result = sbf.toString();}
} catch (MalformedURLException e) {e.printStackTrace();
} catch (IOException e) {e.printStackTrace();
} finally {
// 敞开资源
if (null != br) {
try {br.close();
} catch (IOException e) {e.printStackTrace();
}
}
if (null != os) {
try {os.close();
} catch (IOException e) {e.printStackTrace();
}
}
if (null != is) {
try {is.close();
} catch (IOException e) {e.printStackTrace();
}
}
// 断开与近程地址 url 的连贯
connection.disconnect();}
return result;
}
}
第二种形式:apache HttpClient3.1
package com.powerX.httpClient;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
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.HttpMethodParams;
public class HttpClient3 {public static String doGet(String url) {
// 输出流
InputStream is = null;
BufferedReader br = null;
String result = null;
// 创立 httpClient 实例
HttpClient httpClient = new HttpClient();
// 设置 http 连贯主机服务超时工夫:15000 毫秒
// 先获取连贯管理器对象,再获取参数对象, 再进行参数的赋值
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(15000);
// 创立一个 Get 办法实例对象
GetMethod getMethod = new GetMethod(url);
// 设置 get 申请超时为 60000 毫秒
getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 60000);
// 设置申请重试机制,默认重试次数:3 次,参数设置为 true,重试机制可用,false 相同
getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, true));
try {
// 执行 Get 办法
int statusCode = httpClient.executeMethod(getMethod);
// 判断返回码
if (statusCode != HttpStatus.SC_OK) {
// 如果状态码返回的不是 ok, 阐明失败了, 打印错误信息
System.err.println("Method faild:" + getMethod.getStatusLine());
} else {
// 通过 getMethod 实例,获取近程的一个输出流
is = getMethod.getResponseBodyAsStream();
// 包装输出流
br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
StringBuffer sbf = new StringBuffer();
// 读取封装的输出流
String temp = null;
while ((temp = br.readLine()) != null) {sbf.append(temp).append("\r\n");
}
result = sbf.toString();}
} catch (IOException e) {e.printStackTrace();
} finally {
// 敞开资源
if (null != br) {
try {br.close();
} catch (IOException e) {e.printStackTrace();
}
}
if (null != is) {
try {is.close();
} catch (IOException e) {e.printStackTrace();
}
}
// 开释连贯
getMethod.releaseConnection();}
return result;
}
public static String doPost(String url, Map<String, Object> paramMap) {
// 获取输出流
InputStream is = null;
BufferedReader br = null;
String result = null;
// 创立 httpClient 实例对象
HttpClient httpClient = new HttpClient();
// 设置 httpClient 连贯主机服务器超时工夫:15000 毫秒
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(15000);
// 创立 post 申请办法实例对象
PostMethod postMethod = new PostMethod(url);
// 设置 post 申请超时工夫
postMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 60000);
NameValuePair[] nvp = null;
// 判断参数 map 汇合 paramMap 是否为空
if (null != paramMap && paramMap.size() > 0) {// 不为空
// 创立键值参数对象数组,大小为参数的个数
nvp = new NameValuePair[paramMap.size()];
// 循环遍历参数汇合 map
Set<Entry<String, Object>> entrySet = paramMap.entrySet();
// 获取迭代器
Iterator<Entry<String, Object>> iterator = entrySet.iterator();
int index = 0;
while (iterator.hasNext()) {Entry<String, Object> mapEntry = iterator.next();
// 从 mapEntry 中获取 key 和 value 创立键值对象寄存到数组中
try {nvp[index] = new NameValuePair(mapEntry.getKey(),
new String(mapEntry.getValue().toString().getBytes("UTF-8"), "UTF-8"));
} catch (UnsupportedEncodingException e) {e.printStackTrace();
}
index++;
}
}
// 判断 nvp 数组是否为空
if (null != nvp && nvp.length > 0) {
// 将参数寄存到 requestBody 对象中
postMethod.setRequestBody(nvp);
}
// 执行 POST 办法
try {int statusCode = httpClient.executeMethod(postMethod);
// 判断是否胜利
if (statusCode != HttpStatus.SC_OK) {System.err.println("Method faild:" + postMethod.getStatusLine());
}
// 获取近程返回的数据
is = postMethod.getResponseBodyAsStream();
// 封装输出流
br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
StringBuffer sbf = new StringBuffer();
String temp = null;
while ((temp = br.readLine()) != null) {sbf.append(temp).append("\r\n");
}
result = sbf.toString();} catch (IOException e) {e.printStackTrace();
} finally {
// 敞开资源
if (null != br) {
try {br.close();
} catch (IOException e) {e.printStackTrace();
}
}
if (null != is) {
try {is.close();
} catch (IOException e) {e.printStackTrace();
}
}
// 开释连贯
postMethod.releaseConnection();}
return result;
}
}
第三种形式:apache httpClient4.5
package com.powerX.httpClient;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
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.HttpGet;
import org.apache.http.client.methods.HttpPost;
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;
public class HttpClient4 {public static String doGet(String url) {
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
String result = "";
try {
// 通过址默认配置创立一个 httpClient 实例
httpClient = HttpClients.createDefault();
// 创立 httpGet 近程连贯实例
HttpGet httpGet = new HttpGet(url);
// 设置申请头信息,鉴权
httpGet.setHeader("Authorization", "Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0");
// 设置配置申请参数
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000)// 连贯主机服务超时工夫
.setConnectionRequestTimeout(35000)// 申请超时工夫
.setSocketTimeout(60000)// 数据读取超时工夫
.build();
// 为 httpGet 实例设置配置
httpGet.setConfig(requestConfig);
// 执行 get 申请失去返回对象
response = httpClient.execute(httpGet);
// 通过返回对象获取返回数据
HttpEntity entity = response.getEntity();
// 通过 EntityUtils 中的 toString 办法将后果转换为字符串
result = EntityUtils.toString(entity);
} catch (ClientProtocolException e) {e.printStackTrace();
} catch (IOException e) {e.printStackTrace();
} finally {
// 敞开资源
if (null != response) {
try {response.close();
} catch (IOException e) {e.printStackTrace();
}
}
if (null != httpClient) {
try {httpClient.close();
} catch (IOException e) {e.printStackTrace();
}
}
}
return result;
}
public static String doPost(String url, Map<String, Object> paramMap) {
CloseableHttpClient httpClient = null;
CloseableHttpResponse httpResponse = null;
String result = "";
// 创立 httpClient 实例
httpClient = HttpClients.createDefault();
// 创立 httpPost 近程连贯实例
HttpPost httpPost = new HttpPost(url);
// 配置申请参数实例
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000)// 设置连贯主机服务超时工夫
.setConnectionRequestTimeout(35000)// 设置连贯申请超时工夫
.setSocketTimeout(60000)// 设置读取数据连贯超时工夫
.build();
// 为 httpPost 实例设置配置
httpPost.setConfig(requestConfig);
// 设置申请头
httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");
// 封装 post 申请参数
if (null != paramMap && paramMap.size() > 0) {List<NameValuePair> nvps = new ArrayList<NameValuePair>();
// 通过 map 集成 entrySet 办法获取 entity
Set<Entry<String, Object>> entrySet = paramMap.entrySet();
// 循环遍历,获取迭代器
Iterator<Entry<String, Object>> iterator = entrySet.iterator();
while (iterator.hasNext()) {Entry<String, Object> mapEntry = iterator.next();
nvps.add(new BasicNameValuePair(mapEntry.getKey(), mapEntry.getValue().toString()));
}
// 为 httpPost 设置封装好的申请参数
try {httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
} catch (UnsupportedEncodingException e) {e.printStackTrace();
}
}
try {
// httpClient 对象执行 post 申请, 并返回响应参数对象
httpResponse = httpClient.execute(httpPost);
// 从响应对象中获取响应内容
HttpEntity entity = httpResponse.getEntity();
result = EntityUtils.toString(entity);
} catch (ClientProtocolException e) {e.printStackTrace();
} catch (IOException e) {e.printStackTrace();
} finally {
// 敞开资源
if (null != httpResponse) {
try {httpResponse.close();
} catch (IOException e) {e.printStackTrace();
}
}
if (null != httpClient) {
try {httpClient.close();
} catch (IOException e) {e.printStackTrace();
}
}
}
return result;
}
}
有时候咱们在应用 post 申请时,可能传入的参数是 json 或者其余格局,此时咱们则须要更改申请头及参数的设置信息,以 httpClient4.5 为例,更改上面两列配置:
httpPost.setEntity(new StringEntity(“ 你的 json 串 ”)); httpPost.addHeader(“Content-Type”, “application/json”)。
微信公众号【程序员黄小斜】作者是前蚂蚁金服 Java 工程师,专一分享 Java 技术干货和求职成长心得,不限于 BAT 面试,算法、计算机根底、数据库、分布式、spring 全家桶、微服务、高并发、JVM、Docker 容器,ELK、大数据等。关注后回复【book】支付精选 20 本 Java 面试必备精品电子书。