MCPcopy Create free account
hub / github.com/OreosLab/bili / HttpUtils

Class HttpUtils

src/main/java/top/misec/utils/HttpUtils.java:35–184  ·  view source on GitHub ↗

http utils. @author Junzhou Liu @since 2020/10/11 4:03

Source from the content-addressed store, hash-verified

33 */
34
35@Slf4j
36@Data
37public class HttpUtils {
38 private static final RequestConfig REQUEST_CONFIG = RequestConfig.custom()
39 .setConnectTimeout(5000)
40 .setConnectionRequestTimeout(5000)
41 .setSocketTimeout(10000)
42 .build();
43 private static String userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36 Edg/93.0.961.38";
44 private static CloseableHttpClient httpClient = null;
45 private static CloseableHttpResponse httpResponse = null;
46
47 public static JsonObject doPost(String url, JsonObject jsonObject) {
48 return doPost(url, jsonObject.toString());
49 }
50
51 public static JsonObject doPost(String url, String requestBody) {
52 return doPost(url, requestBody, null);
53 }
54
55 public static JsonObject doPost(String url, String requestBody, Map<String, String> headers, RequestConfig requestConfig) {
56 httpClient = HttpClients.createDefault();
57 JsonObject resultJson = null;
58 // 创建httpPost远程连接实例
59 HttpPost httpPost = new HttpPost(url);
60 // 设置请求头
61 httpPost.setConfig(requestConfig);
62 /*
63 addHeader:添加一个新的请求头字段。(一个请求头中允许有重名字段。)
64 setHeader:设置一个请求头字段,有则覆盖,无则添加。
65 有什么好的方式判断key1=value和{"key1":"value"}
66 */
67 if (requestBody.startsWith("{")) {
68 //java的正则表达式咋写......
69 httpPost.setHeader("Content-Type", "application/json");
70 } else {
71 httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
72 }
73 httpPost.setHeader("Connection", "keep-alive");
74 httpPost.setHeader("User-Agent", userAgent);
75 httpPost.setHeader("cookie", ConfigLoader.helperConfig.getBiliVerify().getBiliCookies());
76
77 if (null != headers && !headers.isEmpty()) {
78 headers.forEach(httpPost::setHeader);
79 } else {
80 httpPost.setHeader("Referer", "https://www.bilibili.com/");
81 }
82 // 封装post请求参数
83 StringEntity stringEntity = new StringEntity(requestBody, "utf-8");
84
85 httpPost.setEntity(stringEntity);
86
87 try {
88 // httpClient对象执行post请求,并返回响应参数对象
89 httpResponse = httpClient.execute(httpPost);
90 resultJson = processResult(httpResponse);
91 } catch (Exception e) {
92 log.error("", e);

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…