更换请求为hutool
This commit is contained in:
parent
8f41a4f66d
commit
afaac1bb0c
@ -1,6 +1,7 @@
|
|||||||
package com.ycwl.basic.notify;
|
package com.ycwl.basic.notify;
|
||||||
|
|
||||||
import com.ycwl.basic.notify.adapters.INotifyAdapter;
|
import com.ycwl.basic.notify.adapters.INotifyAdapter;
|
||||||
|
import com.ycwl.basic.notify.adapters.ServerChanNotifyAdapter;
|
||||||
import com.ycwl.basic.notify.enums.NotifyType;
|
import com.ycwl.basic.notify.enums.NotifyType;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@ -10,7 +11,7 @@ public class NotifyFactory {
|
|||||||
public static INotifyAdapter get(NotifyType type) {
|
public static INotifyAdapter get(NotifyType type) {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case SERVER_CHAN:
|
case SERVER_CHAN:
|
||||||
return new com.ycwl.basic.notify.adapters.ServerChanNotifyAdapter();
|
return new ServerChanNotifyAdapter();
|
||||||
default:
|
default:
|
||||||
throw new RuntimeException("不支持的通知类型");
|
throw new RuntimeException("不支持的通知类型");
|
||||||
}
|
}
|
||||||
|
@ -1,14 +1,10 @@
|
|||||||
package com.ycwl.basic.notify.adapters;
|
package com.ycwl.basic.notify.adapters;
|
||||||
|
|
||||||
|
import cn.hutool.http.HttpUtil;
|
||||||
import com.ycwl.basic.notify.entity.NotifyContent;
|
import com.ycwl.basic.notify.entity.NotifyContent;
|
||||||
import com.ycwl.basic.notify.entity.ServerChanConfig;
|
import com.ycwl.basic.notify.entity.ServerChanConfig;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.util.HashMap;
|
||||||
import java.io.DataOutputStream;
|
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.net.HttpURLConnection;
|
|
||||||
import java.net.URL;
|
|
||||||
import java.net.URLEncoder;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
@ -46,28 +42,10 @@ public class ServerChanNotifyAdapter implements INotifyAdapter {
|
|||||||
} else {
|
} else {
|
||||||
api = "https://sctapi.ftqq.com/" + key + ".send";
|
api = "https://sctapi.ftqq.com/" + key + ".send";
|
||||||
}
|
}
|
||||||
|
Map<String, Object> body = new HashMap<>();
|
||||||
String body = "text=" + URLEncoder.encode(title, "UTF-8") + "&desp=" + URLEncoder.encode(content, "UTF-8");
|
body.put("title", title);
|
||||||
URL apiUrl = new URL(api);
|
body.put("desp", content);
|
||||||
HttpURLConnection httpConnection = (HttpURLConnection) apiUrl.openConnection();
|
return HttpUtil.post(api, body);
|
||||||
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();
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return null;
|
return null;
|
||||||
|
@ -163,4 +163,4 @@ notify:
|
|||||||
- name: "developer"
|
- name: "developer"
|
||||||
type: "SERVER_CHAN"
|
type: "SERVER_CHAN"
|
||||||
config:
|
config:
|
||||||
key: sctp747tje1xjxbwn2fodgu1qezpn3
|
key: "sctp747ta-wkq4hlzb6e42t8d1sm8wbc9g"
|
@ -165,4 +165,4 @@ notify:
|
|||||||
- name: "developer"
|
- name: "developer"
|
||||||
type: "SERVER_CHAN"
|
type: "SERVER_CHAN"
|
||||||
config:
|
config:
|
||||||
key: sctp747tje1xjxbwn2fodgu1qezpn3
|
key: "sctp747ta-wkq4hlzb6e42t8d1sm8wbc9g"
|
Loading…
x
Reference in New Issue
Block a user