在实际工作中,很多地方都会涉及到跨程序互调的问题;java程序员遇到最多的就是通过http协议互调的问题;关于这个问题的解决方案有很多:
1、apache的HttpClient
2、commons的HttpClient等
注意:如下的程序使用maven导入jar包,如果maven不熟悉,可以找到相应的jar包直接加到classpath下也可以。
这里以apache的HttpClient为例,介绍一下关于如何使用apache的HttpClient发送Get/Post请求
1、程序中引入maven的依赖
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.6</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.3</version>
</dependency>
2、项目中如果大量的使用到这种请求方式,建议大家创建工具类在后面复用。工具类代码如下;如下工具类中包含通过HttpClient发送Get/Post请求,传递json参数、普通文本参数、图片+普通文本参数混传;导包的时候注意不要导错。
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
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.client.utils.URIBuilder;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.StringBody;
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.CharArrayBuffer;
import org.apache.http.util.EntityUtils;
public class HttpClientUtils {
/**
* 发送post请求,参数用map接收
*
* @param url
* 地址
* @param map
* 参数
* @return 返回值
*/
public static String postMap(String url, Map<String, String> map) {
String result = null;
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost post = new HttpPost(url);
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> entry : map.entrySet()) {
pairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
CloseableHttpResponse response = null;
try {
post.setEntity(new UrlEncodedFormEntity(pairs, "UTF-8"));
response = httpClient.execute(post);
if (response != null && response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
result = entityToString(entity);
}
return result;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
httpClient.close();
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
/**
* get请求,参数拼接在地址上
*
* @param url
* 请求地址加参数
* @return 响应
*/
public static String get(String url) {
String result = null;
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet get = new HttpGet(url);
CloseableHttpResponse response = null;
try {
response = httpClient.execute(get);
if (response != null && response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
result = entityToString(entity);
}
return result;
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
httpClient.close();
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
/**
* get请求,参数放在map里
*
* @param url
* 请求地址
* @param map
* 参数map
* @return 响应
*/
public static String get(String url, Map<String, String> map) {
String result = null;
CloseableHttpClient httpClient = HttpClients.createDefault();
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> entry : map.entrySet()) {
pairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
CloseableHttpResponse response = null;
try {
URIBuilder builder = new URIBuilder(url);
builder.setParameters(pairs);
HttpGet get = new HttpGet(builder.build());
response = httpClient.execute(get);
if (response != null && response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
result = entityToString(entity);
}
return result;
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
httpClient.close();
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
/**
* post请求,参数为json字符串
*
* @param url
* 请求地址
* @param jsonString
* json字符串
* @return 响应
*/
public static String postJson(String url, String jsonString) {
String result = null;
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost post = new HttpPost(url);
CloseableHttpResponse response = null;
try {
post.setEntity(new ByteArrayEntity(jsonString.getBytes("UTF-8")));
response = httpClient.execute(post);
if (response != null && response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
result = entityToString(entity);
}
return result;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
httpClient.close();
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
public static void upload(String url, String localFile, Map<String, String> param) {
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
try {
httpClient = HttpClients.createDefault();
// 把一个普通参数和文件上传给下面这个地址 是一个servlet
HttpPost httpPost = new HttpPost(url);
File file = new File(localFile);
// 相当于<input type="file" name="file"/>
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addBinaryBody("pic", new FileInputStream(file), ContentType.DEFAULT_BINARY, file.getName());
for (Map.Entry<String, String> entry : param.entrySet()) {
String key = entry.getKey();
// 相当于<input type="text" name="userName" value=userName>
StringBody value = new StringBody(entry.getValue(), ContentType.create("text/plain", Consts.UTF_8));
builder.addPart(key, value);
}
HttpEntity reqEntity = builder.build();
httpPost.setEntity(reqEntity);
// 发起请求 并返回请求的响应
response = httpClient.execute(httpPost);
System.out.println("The response value of token:" + response.getFirstHeader("token"));
// 获取响应对象
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
// 打印响应长度
System.out.println("Response content length: " + resEntity.getContentLength());
// 打印响应内容
System.out.println(EntityUtils.toString(resEntity, Charset.forName("UTF-8")));
}
// 销毁
EntityUtils.consume(resEntity);
} catch (Exception e) {
System.out.println("出错啦...."+e.getMessage());
e.printStackTrace();
} finally {
try {
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (httpClient != null) {
httpClient.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static String entityToString(HttpEntity entity) throws IOException {
String result = null;
if (entity != null) {
long lenth = entity.getContentLength();
if (lenth != -1 && lenth < 2048) {
result = EntityUtils.toString(entity, "UTF-8");
} else {
InputStreamReader reader1 = new InputStreamReader(entity.getContent(), "UTF-8");
CharArrayBuffer buffer = new CharArrayBuffer(2048);
char[] tmp = new char[1024];
int l;
while ((l = reader1.read(tmp)) != -1) {
buffer.append(tmp, 0, l);
}
result = buffer.toString();
}
}
return result;
}
}
3、模拟程序测试,以下是以请求聚合数据的身份证识别接口为例,演示工具类的使用;如果不了解,请上聚合数据官方网站查阅相关文档,网址:https://www.juhe.cn/,有大量的付费接口
编写一个main函数,加上如下代码;
//请求图片上传结果
String key = "b1e899132c9bf7032bb86d6f9bc240e4";
String url = "http://v.juhe.cn/certificates/query.php";
String cardType = "2";
Map<String,String> param = new HashMap<>();
param.put("key", key);
param.put("cardType", cardType);
HttpClientUtils.upload(url, "C:/Users/luocheng/Desktop/sfz.jpg", param);