概述
请求第三方接口数据
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
package com.example.demo;
import lombok.Data;
/**
* @Author shichaoran
* @Date 2020/4/18 23:35
* @Version
*/
@Data
public class GTResponseBO {
private String publishTime;
}
package demo;
/**
* @Author shichaoran
* @Date 2020/4/17 15:01
* @Version
*/
import java.util.HashMap;
import java.util.Map;
/**
* 请求头
*/
public class HttpHeader {
private Map<String, String> params = new HashMap<String, String>();
public HttpHeader addParam(String name, String value) {
this.params.put(name, value);
return this;
}
public Map<String, String> getParams() {
return this.params;
}
}
package demo;
import org.apache.commons.collections4.Get;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
/**
* @Author shichaoran
* @Date 2020/4/17 15:27
* @Version
*/
public enum HttpMethod {
GET, POST;
}
package demo;
/**
* @Author shichaoran
* @Date 2020/4/17 14:57
* @Version
*/
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import com.alibaba.fastjson.JSON;
import org.springframework.http.HttpMethod;
/**
* 请求参数
*/
public class HttpParamers {
private Map<String, String> params = new HashMap<String, String>();
private HttpMethod httpMethod;
private String jsonParamer = "";
public HttpParamers(HttpMethod httpMethod) {
this.httpMethod = httpMethod;
}
public static HttpParamers httpPostParamers() {
return new HttpParamers(HttpMethod.POST);
}
public static HttpParamers httpGetParamers() {
return new HttpParamers(HttpMethod.GET);
}
public HttpParamers addParam(String name, String value) {
this.params.put(name, value);
return this;
}
public HttpMethod getHttpMethod() {
return this.httpMethod;
}
public String getQueryString(String charset) throws IOException {
if ((this.params == null) || (this.params.isEmpty())) {
return null;
}
StringBuilder query = new StringBuilder();
Set<Map.Entry<String, String>> entries = this.params.entrySet();
for (Map.Entry<String, String> entry : entries) {
String name = entry.getKey();
String value = entry.getValue();
query.append("&").append(name).append("=").append(URLEncoder.encode(value, charset));
}
return query.substring(1);
}
public boolean isJson() {
return !isEmpty(this.jsonParamer);
}
public Map<String, String> getParams() {
return this.params;
}
public String toString() {
return "HttpParamers " + JSON.toJSONString(this);
}
public String getJsonParamer() {
return this.jsonParamer;
}
public void setJsonParamer() {
this.jsonParamer = JSON.toJSONString(this.params);
}
private static boolean isEmpty(CharSequence cs) {
return (cs == null) || (cs.length() == 0);
}
}
*/
public class RemoteDataUtil {
public static String getData() throws Exception {
// 1根据token获取url
String response = "";
List<String> list =new ArrayList<>() ;
String uri = "";
HttpParamers paramers = HttpParamers.httpPostParamers();
paramers.addParam("time", String.valueOf(System.currentTimeMillis()));
HttpService httpService = new HttpService(uri);
response = httpService.service("/posts", paramers);
JSONObject jsonObject = JSONObject.parseObject(response);
Object markets = jsonObject.get("markets");
JSONArray jsonArray = JSON.parseArray(markets.toString());
jsonArray.forEach(json->{
JSONObject jsonObject1 = JSONObject.parseObject(json.toString());
Object urlObject = jsonObject1.get("url");
AtomicReference<String> response1 = new AtomicReference<>("");
HttpService httpService1 = new HttpService(urlObject.toString());
try {
response1.set(httpService1.service(urlObject.toString(),paramers));
} catch (Exception e) {
e.printStackTrace();
}
list.add(response1.get());
});
return list.toString();
}
}
package Service;
/**
* @Author shichaoran
* @Date 2020/4/17 15:03
* @Version
*/
import java.util.Map;
import Client.HttpClient;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import demo.HttpHeader;
import demo.HttpParamers;
import lombok.Data;
import lombok.ToString;
@Data
@ToString
public class HttpService {
private String serverUrl;
private int connectTimeout = 15000;
private int readTimeout = 30000;
public HttpService(String serverUrl) {
this.serverUrl = serverUrl.trim();
}
public Map<String, Object> commonService(String serviceUrl, HttpParamers paramers) throws Exception {
return commonService(serviceUrl, paramers, null);
}
public Map<String, Object> commonService(String serviceUrl, HttpParamers paramers, HttpHeader header) throws Exception {
String response = service(serviceUrl, paramers, header);
try {
Map<String, Object> result = JSONObject.parseObject(response, new TypeReference<Map<String, Object>>() {
});
if ((result == null) || (result.isEmpty())) {
throw new Exception("远程服务返回的数据无法解析");
}
Integer code = (Integer) result.get("code");
if ((code == null) || (code.intValue() != 0)) {
throw new Exception((String) result.get("message"));
}
return result;
} catch (Exception e) {
throw new Exception("返回结果异常,response:" + response, e);
}
}
public String service(String serviceUrl, HttpParamers paramers) throws Exception {
return service(serviceUrl, paramers, null);
}
public String service(String serviceUrl, HttpParamers paramers, HttpHeader header) throws Exception {
String url = this.serverUrl + serviceUrl;
String responseData = "";
try {
responseData = HttpClient.doService(url, paramers, header, this.connectTimeout, this.readTimeout);
} catch (Exception e) {
throw new Exception(e.getMessage(), e);
}
return responseData;
}
public String getServerUrl() {
return this.serverUrl;
}
public int getConnectTimeout() {
return this.connectTimeout;
}
public int getReadTimeout() {
return this.readTimeout;
}
public void setConnectTimeout(int connectTimeout) {
this.connectTimeout = connectTimeout;
}
public void setReadTimeout(int readTimeout) {
this.readTimeout = readTimeout;
}
}
package Util;
import org.jsoup.Connection;
import org.jsoup.Connection.Method;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
import java.io.*;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.X509TrustManager;
/**
* Http发送post请求工具,兼容http和https两种请求类型
*/
public class HttpUtils {
/**
* 请求超时时间
*/
private static final int TIME_OUT = 120000;
/**
* Https请求
*/
private static final String HTTPS = "https";
/**
* Content-Type
*/
private static final String CONTENT_TYPE = "Content-Type";
/**
* 表单提交方式Content-Type
*/
private static final String FORM_TYPE = "application/x-www-form-urlencoded;charset=UTF-8";
/**
* JSON提交方式Content-Type
*/
private static final String JSON_TYPE = "application/json;charset=UTF-8";
/**
* 发送Get请求
*
* @param url 请求URL
* @return HTTP响应对象
* @throws IOException 程序异常时抛出,由调用者处理
*/
public static Response get(String url) throws IOException {
return get(url, null);
}
/**
* 发送Get请求
*
* @param url 请求URL
* @param headers 请求头参数
* @return HTTP响应对象
* @throws IOException 程序异常时抛出,由调用者处理
*/
public static Response get(String url, Map<String, String> headers) throws IOException {
if (null == url || url.isEmpty()) {
throw new RuntimeException("The request URL is blank.");
}
// 如果是Https请求
if (url.startsWith(HTTPS)) {
getTrust();
}
Connection connection = Jsoup.connect(url);
connection.method(Connection.Method.GET);
connection.timeout(TIME_OUT);
connection.ignoreHttpErrors(true);
connection.ignoreContentType(true);
connection.maxBodySize(0);
if (null != headers) {
connection.headers(headers);
}
Response response = connection.execute();
return response;
}
/**
* 发送JSON格式参数POST请求
*
* @param url 请求路径
* @param params JSON格式请求参数
* @return HTTP响应对象
* @throws IOException 程序异常时抛出,由调用者处理
*/
public static Response post(String url, String params) throws IOException {
return doPostRequest(url, null, params);
}
/**
* 发送JSON格式参数POST请求
*
* @param url 请求路径
* @param headers 请求头中设置的参数
* @param params JSON格式请求参数
* @return HTTP响应对象
* @throws IOException 程序异常时抛出,由调用者处理
*/
public static Response post(String url, Map<String, String> headers, String params) throws IOException {
return doPostRequest(url, headers, params);
}
/**
* 字符串参数post请求
*
* @param url 请求URL地址
* @param paramMap 请求字符串参数集合
* @return HTTP响应对象
* @throws IOException 程序异常时抛出,由调用者处理
*/
public static Response post(String url, Map<String, String> paramMap) throws IOException {
return doPostRequest(url, null, paramMap, null);
}
/**
* 带请求头的普通表单提交方式post请求
*
* @param headers 请求头参数
* @param url 请求URL地址
* @param paramMap 请求字符串参数集合
* @return HTTP响应对象
* @throws IOException 程序异常时抛出,由调用者处理
*/
public static Response post(Map<String, String> headers, String url, Map<String, String> paramMap) throws IOException {
return doPostRequest(url, headers, paramMap, null);
}
/**
* 带上传文件的post请求
*
* @param url 请求URL地址
* @param paramMap 请求字符串参数集合
* @param fileMap 请求文件参数集合
* @return HTTP响应对象
* @throws IOException 程序异常时抛出,由调用者处理
*/
public static Response post(String url, Map<String, String> paramMap, Map<String, File> fileMap) throws IOException {
return doPostRequest(url, null, paramMap, fileMap);
}
/**
* 带请求头的上传文件post请求
*
* @param url 请求URL地址
* @param headers 请求头参数
* @param paramMap 请求字符串参数集合
* @param fileMap 请求文件参数集合
* @return HTTP响应对象
* @throws IOException 程序异常时抛出,由调用者处理
*/
public static Response post(String url, Map<String, String> headers, Map<String, String> paramMap, Map<String, File> fileMap) throws IOException {
return doPostRequest(url, headers, paramMap, fileMap);
}
/**
* 执行post请求
*
* @param url 请求URL地址
* @param headers 请求头
* @param jsonParams 请求JSON格式字符串参数
* @return HTTP响应对象
* @throws IOException 程序异常时抛出,由调用者处理
*/
private static Response doPostRequest(String url, Map<String, String> headers, String jsonParams) throws IOException {
if (null == url || url.isEmpty()) {
throw new RuntimeException("The request URL is blank.");
}
// 如果是Https请求
if (url.startsWith(HTTPS)) {
getTrust();
}
Connection connection = Jsoup.connect(url);
connection.method(Method.POST);
connection.timeout(TIME_OUT);
connection.ignoreHttpErrors(true);
connection.ignoreContentType(true);
connection.maxBodySize(0);
if (null != headers) {
connection.headers(headers);
}
connection.header(CONTENT_TYPE, JSON_TYPE);
connection.requestBody(jsonParams);
Response response = connection.method(Connection.Method.POST).execute();
/*Response response = connection.execute();*/
return response;
}
/**
* 普通表单方式发送POST请求
*
* @param url 请求URL地址
* @param headers 请求头
* @param paramMap 请求字符串参数集合
* @param fileMap 请求文件参数集合
* @return HTTP响应对象
* @throws IOException 程序异常时抛出,由调用者处理
*/
private static Response doPostRequest(String url, Map<String, String> headers, Map<String, String> paramMap, Map<String, File> fileMap) throws IOException {
if (null == url || url.isEmpty()) {
throw new RuntimeException("The request URL is blank.");
}
// 如果是Https请求
if (url.startsWith(HTTPS)) {
getTrust();
}
Connection connection = Jsoup.connect(url);
connection.method(Method.POST);
connection.timeout(TIME_OUT);
connection.ignoreHttpErrors(true);
connection.ignoreContentType(true);
connection.maxBodySize(0);
if (null != headers) {
connection.headers(headers);
}
// 收集上传文件输入流,最终全部关闭
List<InputStream> inputStreamList = null;
try {
// 添加文件参数
if (null != fileMap && !fileMap.isEmpty()) {
inputStreamList = new ArrayList<InputStream>();
InputStream in = null;
File file = null;
for (Entry<String, File> e : fileMap.entrySet()) {
file = e.getValue();
in = new FileInputStream(file);
inputStreamList.add(in);
connection.data(e.getKey(), file.getName(), in);
}
}
// 普通表单提交方式
else {
connection.header(CONTENT_TYPE, FORM_TYPE);
}
// 添加字符串类参数
if (null != paramMap && !paramMap.isEmpty()) {
connection.data(paramMap);
}
Response response = connection.execute();
return response;
}
// 关闭上传文件的输入流
finally {
closeStream(inputStreamList);
}
}
/**
* 关流
*
* @param streamList 流集合
*/
private static void closeStream(List<? extends Closeable> streamList) {
if (null != streamList) {
for (Closeable stream : streamList) {
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 获取服务器信任
*/
private static void getTrust() {
try {
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, new X509TrustManager[] { new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {}
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {}
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
} }, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.example.demo;
/**
* @Author shichaoran
* @Date 2020/4/17 15:04
* @Version
*/
import com.alibaba.fastjson.JSONObject;
import demo.HttpParamers;
import Service.HttpService;
import demo.RemoteDataUtil;
import org.junit.Ignore;
import org.junit.Test;
import java.util.List;
public class Test1 {
String uri = "http://open.api.mysteel.cn/zjsj/api_marketl_zjsjl.html?page=3&tableId=15608&pageSize=2&token=IgtdMORZvrU7BZ4PGBBPLK34U4cNuB5F&startTime=2020-01-01&endTime=2020-04-30";
//
//get方式请求数据
//
//请求地址:http://jsonplaceholder.typicode.com/posts
//
@Ignore("lue")
//
@Test
//
public void test1() {
//
System.out.print("n" + "test1---------------------------" + "n");
//
HttpParamers paramers = HttpParamers.httpGetParamers();
//
//
try {
//
HttpService httpService = new HttpService(uri);
//
String response = httpService.service("/posts", paramers);
//
JSONObject jsonObject = JSONObject.parseObject(response);
//
List<JSONObject> list = (List) jsonObject.get("markets");
//
for (JSONObject object : list) {
//
System.out.println(object.get("url"));
//
}
System.out.println("response: " + JSON.toJSONString(jsonObject));
//
} catch (Exception e) {
//
e.printStackTrace();
//
}
//
//
}
//
String uris = "http://open.api.mysteel.cn/zjsj/api_marketd_zjsjd.html?token=IgtdMORZvrU7BZ4PGBBPLK34U4cNuB5F&id=gn1vepj2he5di";
//
//
//get方式请求数据
//
//请求地址:http://jsonplaceholder.typicode.com/posts?userId=5
//
@Ignore("lue")
//
@Test
//
public void test2() {
//
System.out.print("n" + "test2---------------------------" + "n");
//
HttpParamers paramers = HttpParamers.httpGetParamers();
//
paramers.addParam("userId", "5");
//
String response = "";
//
try {
//
HttpService httpService = new HttpService(uris);
//
response = httpService.service("/posts", paramers);
//
} catch (Exception e) {
//
e.printStackTrace();
//
}
//
System.out.print(response);
//
}
//post方式请求数据
//请求地址:http://jsonplaceholder.typicode.com/posts
@Test
public void test3() throws Exception {
System.out.print("n" + "test3---------------------------" + "n");
String data = RemoteDataUtil.getData();
System.out.println(data);
}
public static void main(String[] args) {
System.out.println();
}
}
最后
以上就是独特棉花糖为你收集整理的接入第三方数据到mysql的全部内容,希望文章能够帮你解决接入第三方数据到mysql所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复