广告回调调通/http工具类优化

This commit is contained in:
kuang.yife 2023-07-31 11:49:29 +08:00
parent e1d90b795a
commit 3528bb88b3
3 changed files with 82 additions and 7 deletions

View File

@ -12,6 +12,7 @@ import com.ruoyi.common.utils.CacheUtils;
import com.ruoyi.common.utils.ExceptionUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.common.utils.security.Md5Utils;
import com.ruoyi.system.domain.ClewPhone;
import com.ruoyi.system.domain.NoticeRequest;
import com.ruoyi.system.domain.OppoCheck;
@ -32,7 +33,9 @@ import org.apache.commons.codec.binary.Base64;
import javax.validation.Valid;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author clunt
@ -134,11 +137,12 @@ public class NoticeController {
model.setOuId(encode(model.getOuId().getBytes()));
String content = JSONObject.toJSONString(model) + model.getTimestamp() + "e0u6fnlag06lc3pl";
log.info("请求的content加密前属性{}", content);
JSONObject oppoModel = new JSONObject();
oppoModel.put("signature", encode(content.getBytes()));
oppoModel.put("timestamp", model.getTimestamp());
log.info("请求https://api.ads.heytapmobi.com/api/uploadActiveData的属性{}", oppoModel.toJSONString());
String s = HttpUtils.sendPost("https://api.ads.heytapmobi.com/api/uploadActiveData", oppoModel.toJSONString());
Map<String, String> headerMap = new HashMap<>();
headerMap.put("signature", Md5Utils.hash(content));
headerMap.put("timestamp", String.valueOf(model.getTimestamp()));
headerMap.put("Content-Type", "application/json");
log.info("请求https://api.ads.heytapmobi.com/api/uploadActiveData的属性{}", JSONObject.toJSONString(model));
String s = HttpUtils.sendPost("https://api.ads.heytapmobi.com/api/uploadActiveData", JSONObject.toJSONString(model), headerMap);
log.info("请求https://api.ads.heytapmobi.com/api/uploadActiveData的响应{}", s);
}catch (Exception e){
e.printStackTrace();

View File

@ -11,12 +11,15 @@ import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.security.cert.X509Certificate;
import java.util.Map;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ruoyi.common.constant.Constants;
@ -117,6 +120,74 @@ public class HttpUtils
return result.toString();
}
public static String sendPost(String url, String param, Map<String,String> headerMap){
PrintWriter out = null;
BufferedReader in = null;
StringBuilder result = new StringBuilder();
try
{
log.info("sendPost - {}", url);
URL realUrl = new URL(url);
URLConnection conn = realUrl.openConnection();
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
conn.setRequestProperty("Accept-Charset", "utf-8");
conn.setRequestProperty("contentType", "utf-8");
if(!headerMap.isEmpty()){
for (Map.Entry<String, String> entry : headerMap.entrySet()) {
conn.setRequestProperty(entry.getKey(), entry.getValue());
}
}
conn.setDoOutput(true);
conn.setDoInput(true);
out = new PrintWriter(conn.getOutputStream());
out.print(param);
out.flush();
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));
String line;
while ((line = in.readLine()) != null)
{
result.append(line);
}
log.info("recv - {}", result);
}
catch (ConnectException e)
{
log.error("调用HttpUtils.sendPost ConnectException, url=" + url + ",param=" + param, e);
}
catch (SocketTimeoutException e)
{
log.error("调用HttpUtils.sendPost SocketTimeoutException, url=" + url + ",param=" + param, e);
}
catch (IOException e)
{
log.error("调用HttpUtils.sendPost IOException, url=" + url + ",param=" + param, e);
}
catch (Exception e)
{
log.error("调用HttpsUtil.sendPost Exception, url=" + url + ",param=" + param, e);
}
finally
{
try
{
if (out != null)
{
out.close();
}
if (in != null)
{
in.close();
}
}
catch (IOException ex)
{
log.error("调用in.close Exception, url=" + url + ",param=" + param, ex);
}
}
return result.toString();
}
/**
* 向指定 URL 发送POST方法的请求
*

View File

@ -15,8 +15,8 @@ public class OppoCheck {
private Integer dataType = 1;
private Integer channel = 1;
private Integer type = 1;
private Integer ascribeType = 1;
private Long adId = 101097648L;
private Integer ascribeType = 0;
// private Long adId = 101097648L;
}