一、概述
在实际开发过程中,我们经常需要调用对方提供的接口或测试自己写的接口是否合适。很多项目都会封装规定好本身项目的接口规范,所以大多数需要去调用对方提供的接口或第三方接口(短信、天气等)。
在Java项目中调用第三方接口的方式有:
1、通过JDK网络类Java.net.HttpURLConnection;
2、通过common封装好的HttpClient;
3、通过Apache封装好的CloseableHttpClient;
4、通过SpringBoot-RestTemplate;
二、 Java调用第三方http接口的方式
2.1、通过JDK网络类Java.net.HttpURLConnection
比较原始的一种调用做法,这里把get请求和post请求都统一放在一个方法里面。
实现过程:
GET:
1、创建远程连接
2、设置连接方式(get、post、put。。。)
3、设置连接超时时间
4、设置响应读取时间
5、发起请求
6、获取请求数据
7、关闭连接
POST:
1、创建远程连接
2、设置连接方式(get、post、put。。。)
3、设置连接超时时间
4、设置响应读取时间
5、当向远程服务器传送数据/写数据时,需要设置为true(setDoOutput)
6、当前向远程服务读取数据时,设置为true,该参数可有可无(setDoInput)
7、设置传入参数的格式:(setRequestProperty)
8、设置鉴权信息:Authorization:(setRequestProperty)
9、设置参数
10、发起请求
11、获取请求数据
12、关闭连接
直接上代码:
package com.riemann.springbootdemo.util.common.httpConnectionUtil;
import org.springframework.lang.Nullable;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class HttpURLConnectionUtil {
public static String doGet(String httpUrl){
//链接
HttpURLConnection connection = null;
InputStream is = null;
BufferedReader br = null;
StringBuffer result = new StringBuffer();
try {
//创建连接
URL url = new URL(httpUrl);
connection = (HttpURLConnection) url.openConnection();
//设置请求方式
connection.setRequestMethod("GET");
//设置连接超时时间
connection.setReadTimeout(15000);
//开始连接
connection.connect();
//获取响应数据
if (connection.getResponseCode() == 200) {
//获取返回的数据
is = connection.getInputStream();
if (null != is) {
br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String temp = null;
while (null != (temp = br.readLine())) {
result.append(temp);
}
}
}
} 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.toString();
}
public static String doPost(String httpUrl, @Nullable String param) {
StringBuffer result = new StringBuffer();
//连接
HttpURLConnection connection = null;
OutputStream os = null;
InputStream is = null;
BufferedReader br = null;
try {
//创建连接对象
URL url = new URL(httpUrl);
//创建连接
connection = (HttpURLConnection) url.openConnection();
//设置请求方法
connection.setRequestMethod("POST");
//设置连接超时时间
connection.setConnectTimeout(15000);
//设置读取超时时间
connection.setReadTimeout(15000);
//DoOutput设置是否向httpUrlConnection输出,DoInput设置是否从httpUrlConnection读入,此外发送post请求必须设置这两个
//设置是否可读取
connection.setDoOutput(true);
connection.setDoInput(true);
//设置通用的请求属性
connection.setRequestProperty("accept", "*
public class HttpClientUtil {
public static String doGet(String url, String charset) {
//1.生成HttpClient对象并设置参数
HttpClient httpClient = new HttpClient();
//设置Http连接超时为5秒
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
//2.生成GetMethod对象并设置参数
GetMethod getMethod = new GetMethod(url);
//设置get请求超时为5秒
getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 5000);
//设置请求重试处理,用的是默认的重试处理:请求三次
getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
String response = "";
//3.执行HTTP GET 请求
try {
int statusCode = httpClient.executeMethod(getMethod);
//4.判断访问的状态码
if (statusCode != HttpStatus.SC_OK) {
System.err.println("请求出错:" + getMethod.getStatusLine());
}
//5.处理HTTP响应内容
//HTTP响应头部信息,这里简单打印
Header[] headers = getMethod.getResponseHeaders();
for(Header h : headers) {
System.out.println(h.getName() + "---------------" + h.getValue());
}
//读取HTTP响应内容,这里简单打印网页内容
//读取为字节数组
byte[] responseBody = getMethod.getResponseBody();
response = new String(responseBody, charset);
System.out.println("-----------response:" + response);
//读取为InputStream,在网页内容数据量大时候推荐使用
//InputStream response = getMethod.getResponseBodyAsStream();
} catch (HttpException e) {
//发生致命的异常,可能是协议不对或者返回的内容有问题
System.out.println("请检查输入的URL!");
e.printStackTrace();
} catch (IOException e) {
//发生网络异常
System.out.println("发生网络异常!");
} finally {
//6.释放连接
getMethod.releaseConnection();
}
return response;
}
public static String doPost(String url, JSONObject json){
HttpClient httpClient = new HttpClient();
PostMethod postMethod = new PostMethod(url);
postMethod.addRequestHeader("accept", "*
public class CloseableHttpClientUtil {
private static String tokenString = "";
private static String AUTH_TOKEN_EXPIRED = "AUTH_TOKEN_EXPIRED";
private static CloseableHttpClient httpClient = null;
public static String doGet(String url, String token) {
//创建HttpClient对象
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpGet httpGet = new HttpGet(url);
if (null != tokenString && !tokenString.equals("")) {
tokenString = getToken();
}
//api_gateway_auth_token自定义header头,用于token验证使用
httpGet.addHeader("api_gateway_auth_token",tokenString);
httpGet.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36");
try {
HttpResponse response = httpClient.execute(httpGet);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
//返回json格式
String res = EntityUtils.toString(response.getEntity());
return res;
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static String doPost(String url, JSONObject json) {
if (null == httpClient) {
httpClient = HttpClientBuilder.create().build();
}
HttpPost httpPost = new HttpPost(url);
if (null != tokenString && tokenString.equals("")) {
tokenString = getToken();
}
//api_gateway_auth_token自定义header头,用于token验证使用
httpPost.addHeader("api_gateway_auth_token", tokenString);
httpPost.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36");
try {
StringEntity se = new StringEntity(json.toString());
se.setContentEncoding("UTF-8");
//发送json数据需要设置contentType
se.setContentType("application/x-www-form-urlencoded");
//设置请求参数
httpPost.setEntity(se);
HttpResponse response = httpClient.execute(httpPost);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
//返回json格式
String res = EntityUtils.toString(response.getEntity());
return res;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (httpClient != null){
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
public static String getToken() {
String token = "";
JSONObject object = new JSONObject();
object.put("appid", "appid");
object.put("secretkey", "secretkey");
if (null == httpClient) {
httpClient = HttpClientBuilder.create().build();
}
HttpPost httpPost = new HttpPost("http://localhost/login");
httpPost.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36");
try {
StringEntity se = new StringEntity(object.toString());
se.setContentEncoding("UTF-8");
//发送json数据需要设置contentType
se.setContentType("application/x-www-form-urlencoded");
//设置请求参数
httpPost.setEntity(se);
HttpResponse response = httpClient.execute(httpPost);
//这里可以把返回的结果按照自定义的返回数据结果,把string转换成自定义类
//ResultTokenBO result = JSONObject.parseObject(response, ResultTokenBO.class);
//把response转为jsonObject
JSONObject result = (JSONObject) JSONObject.parseObject(String.valueOf(response));
if (result.containsKey("token")) {
token = result.getString("token");
}
} catch (IOException e) {
e.printStackTrace();
}
return token;
}
public static void test(String telephone) {
JSONObject object = new JSONObject();
object.put("telephone", telephone);
//首先获取token
tokenString = getToken();
String response = doPost("http://localhost/searchUrl", object);
//如果返回的结果是list形式的,需要使用JSONObject.parseArray转换
//List<Result> list = JSONObject.parseArray(response, Result.class);
System.out.println(response);
}
public static void main(String[] args) {
test("12345678910");
}
}
2.4 通过SpringBoot-RestTemplate
springBoot-RestTemple是上面三种方式的集大成者,代码编写更加简单,目前可以采用的调用第三方接口有:
delete() 在特定的URL上对资源执行HTTP DELETE操作
exchange() 在URL上执行特定的HTTP方法,返回包含对象的ResponseEntity,这个对象是从响应体中映射得到的
execute() 在URL上执行特定的HTTP方法,返回一个从响应体映射得到的对象
getForEntity() 发送一个HTTP GET请求,返回的ResponseEntity包含了响应体所映射成的对象
getForObject() 发送一个HTTP GET请求,返回的请求体将映射为一个对象
postForEntity() POST 数据到一个URL,返回包含一个对象的ResponseEntity,这个对象是从响应体中映射得到的
postForObject() POST 数据到一个URL,返回根据响应体匹配形成的对象
headForHeaders() 发送HTTP HEAD请求,返回包含特定资源URL的HTTP头
optionsForAllow() 发送HTTP OPTIONS请求,返回对特定URL的Allow头信息
postForLocation() POST 数据到一个URL,返回新创建资源的URL
put() PUT 资源到特定的URL
注意:目前标粗的为常用的
首先导入springboot的web包
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
</parent>
<dependencies>
<!--CloseableHttpClient-->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
<!--spring restTemplate-->
<!-- @ConfigurationProperties annotation processing (metadata for IDEs)
生成spring-configuration-metadata.json类,需要引入此类-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
在启动类同包下创建RestTemplateConfig.java类
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate(ClientHttpRequestFactory factory){
return new RestTemplate(factory);
}
@Bean
public ClientHttpRequestFactory simpleClientHttpRequestFactory(){
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setConnectTimeout(15000);
factory.setReadTimeout(5000);
return factory;
}
}
然后在Service类(RestTemplateToInterface )中注入使用
具体代码如下:
import com.alibaba.fastjson.JSONObject;
import com.swordfall.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class RestTemplateToInterface {
@Autowired
private RestTemplate restTemplate;
public User doGetWith1(String url){
ResponseEntity<User> responseEntity = restTemplate.getForEntity(url, User.class);
User user = responseEntity.getBody();
return user;
}
public User doGetWith2(String url){
User user = restTemplate.getForObject(url, User.class);
return user;
}
public String doPostWith1(String url){
User user = new User("小白", 20);
ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, user, String.class);
String body = responseEntity.getBody();
return body;
}
public String doPostWith2(String url){
User user = new User("小白", 20);
String body = restTemplate.postForObject(url, user, String.class);
return body;
}
public String doExchange(String url, Integer age, String name){
//header参数
HttpHeaders headers = new HttpHeaders();
String token = "asdfaf2322";
headers.add("authorization", token);
headers.setContentType(MediaType.APPLICATION_JSON);
//放入body中的json参数
JSONObject obj = new JSONObject();
obj.put("age", age);
obj.put("name", name);
//组装
HttpEntity<JSONObject> request = new HttpEntity<>(obj, headers);
ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, request, String.class);
String body = responseEntity.getBody();
return body;
}
}
2.5 通过okhttp
应大家的响应,okhttp 现在也是蛮流行的,基于手机端很火,这里分享一下OkHttpClient客户端,业务代码get、post请求直接调用就好哈。
pom文件引入依赖包
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.10.0</version>
</dependency>
@Slf4j
public class OkHttpClient {
private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
private volatile static okhttp3.OkHttpClient client;
private static final int MAX_IDLE_CONNECTION = Integer
.parseInt(ConfigManager.get("httpclient.max_idle_connection"));
private static final long KEEP_ALIVE_DURATION = Long
.parseLong(ConfigManager.get("httpclient.keep_alive_duration"));
private static final long CONNECT_TIMEOUT = Long.parseLong(ConfigManager.get("httpclient.connectTimeout"));
private static final long READ_TIMEOUT = Long.parseLong(ConfigManager.get("httpclient. "));
private static okhttp3.OkHttpClient getInstance() {
if (client == null) {
synchronized (okhttp3.OkHttpClient.class) {
if (client == null) {
client = new okhttp3.OkHttpClient.Builder()
.connectTimeout(CONNECT_TIMEOUT, TimeUnit.SECONDS)
.readTimeout(READ_TIMEOUT, TimeUnit.SECONDS)
.connectionPool(new ConnectionPool(MAX_IDLE_CONNECTION, KEEP_ALIVE_DURATION,
TimeUnit.MINUTES))
.build();
}
}
}
return client;
}
public static String syncPost(String url, String json) throws IOException {
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
try {
Response response = OkHttpClient.getInstance().newCall(request).execute();
if (response.isSuccessful()) {
String result = response.body().string();
log.info("syncPost response = {}, responseBody= {}", response, result);
return result;
}
String result = response.body().string();
log.info("syncPost response = {}, responseBody= {}", response, result);
throw new IOException("三方接口返回http状态码为" + response.code());
} catch (Exception e) {
log.error("syncPost() url:{} have a ecxeption {}", url, e);
throw new RuntimeException("syncPost() have a ecxeption {}" + e.getMessage());
}
}
public static String syncGet(String url, Map<String, Object> headParamsMap) throws IOException {
Request request;
final Request.Builder builder = new Request.Builder().url(url);
try {
if (!CollectionUtils.isEmpty(headParamsMap)) {
final Iterator<Map.Entry<String, Object>> iterator = headParamsMap.entrySet()
.iterator();
while (iterator.hasNext()) {
final Map.Entry<String, Object> entry = iterator.next();
builder.addHeader(entry.getKey(), (String) entry.getValue());
}
}
request = builder.build();
Response response = OkHttpClient.getInstance().newCall(request).execute();
String result = response.body().string();
log.info("syncGet response = {},responseBody= {}", response, result);
if (!response.isSuccessful()) {
throw new IOException("三方接口返回http状态码为" + response.code());
}
return result;
} catch (Exception e) {
log.error("remote interface url:{} have a ecxeption {}", url, e);
throw new RuntimeException("三方接口返回异常");
}
}
}
到此这篇关于利用Java实现调用http请求的文章就介绍到这了,更多相关Java调用http请求内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!