package com.fzzy.push.gd2023;
|
|
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.TypeReference;
|
import com.fzzy.api.entity.ApiConfs;
|
import com.fzzy.api.utils.AESUtils;
|
import com.fzzy.api.utils.MyMD5Util;
|
import com.fzzy.api.utils.SnowflakeIdWorker;
|
import com.fzzy.push.gd2023.dto.GD2023AuthToken;
|
import com.fzzy.push.gd2023.dto.GD2023ResponseDto;
|
import lombok.extern.slf4j.Slf4j;
|
import java.io.*;
|
import java.lang.reflect.Type;
|
import java.net.HttpURLConnection;
|
import java.net.URL;
|
import java.util.Map;
|
import java.util.UUID;
|
|
/**
|
* @author: vincexu
|
* @ClassName: HttpClientUtil.java
|
*/
|
@Slf4j
|
public class GD2023HttpClientUtil {
|
|
|
/**
|
* post formData
|
* @param url
|
* @param map
|
* @return
|
* @throws Exception
|
*/
|
@SuppressWarnings("resource")
|
public static GD2023ResponseDto postGetToken(String url, Map<String, Object> map) throws Exception {
|
log.info("---------接口请求地址:" +url+ "----------参数:" + JSON.toJSONString(map) +"---------");
|
BufferedReader in = null;
|
URL urls = new URL(url);
|
HttpURLConnection connection = null;
|
OutputStream outputStream = null;
|
String rs = "";
|
GD2023ResponseDto responseDto;
|
try {
|
connection = (HttpURLConnection) urls.openConnection();
|
connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
|
connection.setDoOutput(true);
|
connection.setDoInput(true);
|
connection.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8");
|
connection.setRequestProperty("Accept", "*/*");
|
connection.setRequestProperty("Range", "bytes=" + "");
|
connection.setConnectTimeout(20000);
|
connection.setReadTimeout(30000);
|
connection.setRequestMethod("POST");
|
|
StringBuffer buffer = new StringBuffer();
|
outputStream = connection.getOutputStream();
|
buffer.append(JSON.toJSONString(map));
|
outputStream.write(buffer.toString().getBytes());
|
try {
|
connection.connect();
|
if (connection.getResponseCode() == 200) {
|
in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
|
String line = "";
|
while ((line = in.readLine()) != null) {
|
rs += line;
|
}
|
}else{
|
log.error("http状态:" + connection.getResponseCode());
|
log.error("http消息:" + connection.getResponseMessage());
|
}
|
|
} catch (Exception e) {
|
System.out.println("发生异常");
|
log.error(e.getMessage(),e);
|
rs = null;
|
return new GD2023ResponseDto(99,e.getMessage());
|
}
|
log.info("---------接口返回:" + rs +"---------");
|
|
Type type = new TypeReference<GD2023ResponseDto<GD2023AuthToken>>() {}.getType();
|
responseDto = JSON.parseObject(rs,type);
|
if(responseDto == null ) return new GD2023ResponseDto(99,"接口请求发生未知错误");
|
return responseDto;
|
} finally {
|
try {
|
outputStream.close();
|
if (in != null){
|
in.close();
|
}
|
} catch (Exception e) {
|
}
|
outputStream = null;
|
if (connection != null)
|
connection.disconnect();
|
connection = null;
|
}
|
}
|
|
|
/**
|
* post formData
|
* @param url
|
* @return
|
* @throws Exception
|
*/
|
@SuppressWarnings("resource")
|
public static GD2023ResponseDto postPushData(String url, String data , ApiConfs apiConfs, GD2023AuthToken authToken) throws Exception {
|
log.info("---------接口请求地址:" +url+ "----------参数:" + data +"---------");
|
BufferedReader in = null;
|
URL urls = new URL(url);
|
HttpURLConnection connection = null;
|
OutputStream outputStream = null;
|
String rs = "";
|
GD2023ResponseDto responseDto;
|
try {
|
String md = AESUtils.encrypt(data,apiConfs.getPublicKey());
|
log.info("---------接口请求地址:" +url+ "----------密文参数:" + md +"---------");
|
connection = (HttpURLConnection) urls.openConnection();
|
connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
|
connection.setDoOutput(true);
|
connection.setDoInput(true);
|
connection.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8");
|
connection.setRequestProperty("Accept", "*/*");
|
connection.setRequestProperty("Range", "bytes=" + "");
|
connection.setRequestProperty("Authorization", "Bearer " + authToken.getAccess_token() );
|
connection.setRequestProperty("nonce", AESUtils.encrypt(authToken.getAccess_token() + System.currentTimeMillis(),apiConfs.getPublicKey()));
|
connection.setRequestProperty("requestId", AESUtils.encrypt(authToken.getAccess_token() + SnowflakeIdWorker.nextId(),apiConfs.getPublicKey()));
|
connection.setRequestProperty("digest", MyMD5Util.encrypt(data));
|
connection.setConnectTimeout(20000);
|
connection.setReadTimeout(30000);
|
connection.setRequestMethod("POST");
|
StringBuffer buffer = new StringBuffer();
|
buffer.append(md);
|
outputStream = connection.getOutputStream();
|
outputStream.write(buffer.toString().getBytes("UTF-8"));
|
try {
|
connection.connect();
|
if (connection.getResponseCode() == 200) {
|
in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
|
String line = "";
|
while ((line = in.readLine()) != null) {
|
rs += line;
|
}
|
}else{
|
log.error("http状态:" + connection.getResponseCode());
|
log.error("http消息:" + connection.getResponseMessage());
|
}
|
|
} catch (Exception e) {
|
System.out.println("发生异常");
|
log.error(e.getMessage(),e);
|
rs = null;
|
return new GD2023ResponseDto(99,e.getMessage());
|
}
|
log.info("---------接口返回:" + rs +"---------");
|
responseDto = JSON.parseObject(rs,GD2023ResponseDto.class);
|
if(responseDto == null ) return new GD2023ResponseDto(99,"接口请求发生未知错误");
|
return responseDto;
|
} finally {
|
try {
|
outputStream.close();
|
if (in != null){
|
in.close();
|
}
|
} catch (Exception e) {
|
}
|
outputStream = null;
|
if (connection != null)
|
connection.disconnect();
|
connection = null;
|
}
|
}
|
|
/**
|
* 文件上传接口:上传文件,获取文件id
|
* @param url
|
* @return
|
* @throws Exception
|
*/
|
@SuppressWarnings("resource")
|
public static GD2023ResponseDto postUploadData(String url, GD2023AuthToken authToken, String filePath) throws Exception {
|
url = url + "file/upload";
|
log.info("---------接口请求地址:" + url + "----------文件路径:" + filePath + "---------");
|
BufferedReader in = null;
|
URL urls = new URL(url);
|
HttpURLConnection connection = null;
|
OutputStream outputStream = null;
|
InputStream inputStream = null;
|
String rs = "";
|
GD2023ResponseDto responseDto;
|
try {
|
|
connection = (HttpURLConnection) urls.openConnection();
|
connection.setConnectTimeout(20000);
|
connection.setReadTimeout(30000);
|
connection.setDoOutput(true);
|
connection.setDoInput(true);
|
connection.setUseCaches(false);
|
connection.setRequestMethod("POST");
|
connection.setRequestProperty("connection", "Keep-Alive");
|
connection.setRequestProperty("Charset", "UTF-8");
|
|
String boundary = "----" + UUID.randomUUID().toString();
|
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
|
connection.setRequestProperty("Authorization", "Bearer " + authToken.getAccess_token() );
|
|
outputStream = connection.getOutputStream();
|
if(null != filePath){
|
StringBuffer strBuf = new StringBuffer();
|
|
strBuf.append("--").append(boundary).append("\r\n");
|
strBuf.append("Content-Disposition: form-data;name=file;filename=\"" + filePath + "\"\r\n");
|
strBuf.append("\r\n");
|
outputStream.write(strBuf.toString().getBytes());
|
File file = new File(filePath);
|
inputStream = new FileInputStream(file);
|
int bytes = 0;
|
byte[] bufferOut = new byte[1024];
|
while ((bytes = inputStream.read(bufferOut)) != -1) {
|
outputStream.write(bufferOut, 0, bytes);
|
}
|
inputStream.close();
|
|
byte[] endData = ("\r\n--" + boundary + "--\r\n").getBytes();
|
outputStream.write(endData);
|
outputStream.flush();
|
outputStream.close();
|
}
|
|
try {
|
connection.connect();
|
if (connection.getResponseCode() == 200) {
|
in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
|
String line = "";
|
while ((line = in.readLine()) != null) {
|
rs += line;
|
}
|
}else{
|
log.error("http状态:" + connection.getResponseCode());
|
log.error("http消息:" + connection.getResponseMessage());
|
}
|
|
} catch (Exception e) {
|
System.out.println("发生异常");
|
log.error(e.getMessage(),e);
|
rs = null;
|
return new GD2023ResponseDto(99,e.getMessage());
|
}
|
log.info("---------接口返回:" + rs +"---------");
|
responseDto = JSON.parseObject(rs,GD2023ResponseDto.class);
|
if(responseDto == null ) return new GD2023ResponseDto(99,"接口请求发生未知错误");
|
return responseDto;
|
} finally {
|
try {
|
outputStream.close();
|
if (in != null){
|
in.close();
|
}
|
} catch (Exception e) {
|
}
|
outputStream = null;
|
if (connection != null)
|
connection.disconnect();
|
connection = null;
|
}
|
}
|
}
|