更换请求为hutool

This commit is contained in:
Jerry Yan 2025-01-05 18:14:35 +08:00
parent 8f41a4f66d
commit afaac1bb0c
4 changed files with 10 additions and 31 deletions

View File

@ -1,6 +1,7 @@
package com.ycwl.basic.notify;
import com.ycwl.basic.notify.adapters.INotifyAdapter;
import com.ycwl.basic.notify.adapters.ServerChanNotifyAdapter;
import com.ycwl.basic.notify.enums.NotifyType;
import java.util.HashMap;
@ -10,7 +11,7 @@ public class NotifyFactory {
public static INotifyAdapter get(NotifyType type) {
switch (type) {
case SERVER_CHAN:
return new com.ycwl.basic.notify.adapters.ServerChanNotifyAdapter();
return new ServerChanNotifyAdapter();
default:
throw new RuntimeException("不支持的通知类型");
}

View File

@ -1,14 +1,10 @@
package com.ycwl.basic.notify.adapters;
import cn.hutool.http.HttpUtil;
import com.ycwl.basic.notify.entity.NotifyContent;
import com.ycwl.basic.notify.entity.ServerChanConfig;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -46,28 +42,10 @@ public class ServerChanNotifyAdapter implements INotifyAdapter {
} else {
api = "https://sctapi.ftqq.com/" + key + ".send";
}
String body = "text=" + URLEncoder.encode(title, "UTF-8") + "&desp=" + URLEncoder.encode(content, "UTF-8");
URL apiUrl = new URL(api);
HttpURLConnection httpConnection = (HttpURLConnection) apiUrl.openConnection();
httpConnection.setRequestMethod("POST");
httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpConnection.setDoOutput(true);
DataOutputStream bodyWriter = new DataOutputStream(httpConnection.getOutputStream());
bodyWriter.writeBytes(body);
bodyWriter.flush();
bodyWriter.close();
int responseCode = httpConnection.getResponseCode();
BufferedReader reader = new BufferedReader(new InputStreamReader(httpConnection.getInputStream()));
StringBuilder responseText = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
responseText.append(line);
}
reader.close();
return responseText.toString();
Map<String, Object> body = new HashMap<>();
body.put("title", title);
body.put("desp", content);
return HttpUtil.post(api, body);
} catch (Exception e) {
e.printStackTrace();
return null;

View File

@ -163,4 +163,4 @@ notify:
- name: "developer"
type: "SERVER_CHAN"
config:
key: sctp747tje1xjxbwn2fodgu1qezpn3
key: "sctp747ta-wkq4hlzb6e42t8d1sm8wbc9g"

View File

@ -165,4 +165,4 @@ notify:
- name: "developer"
type: "SERVER_CHAN"
config:
key: sctp747tje1xjxbwn2fodgu1qezpn3
key: "sctp747ta-wkq4hlzb6e42t8d1sm8wbc9g"