|
|
|
@ -5,9 +5,11 @@ import com.xxl.job.core.handler.IJobHandler;
|
|
|
|
|
import com.xxl.job.core.log.XxlJobLogger;
|
|
|
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
|
import java.io.DataOutputStream;
|
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
|
import java.net.URL;
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 跨平台Http任务
|
|
|
|
@ -19,10 +21,35 @@ public class HttpJobHandler extends IJobHandler {
|
|
|
|
|
@Override
|
|
|
|
|
public ReturnT<String> execute(String param) throws Exception {
|
|
|
|
|
|
|
|
|
|
// valid
|
|
|
|
|
// param parse
|
|
|
|
|
if (param==null || param.trim().length()==0) {
|
|
|
|
|
XxlJobLogger.log("URL Empty");
|
|
|
|
|
return FAIL;
|
|
|
|
|
XxlJobLogger.log("param["+ param +"] invalid.");
|
|
|
|
|
return ReturnT.FAIL;
|
|
|
|
|
}
|
|
|
|
|
String[] httpParams = param.split("\n");
|
|
|
|
|
String url = null;
|
|
|
|
|
String method = null;
|
|
|
|
|
String data = null;
|
|
|
|
|
for (String httpParam: httpParams) {
|
|
|
|
|
if (httpParam.startsWith("url:")) {
|
|
|
|
|
url = httpParam.substring(httpParam.indexOf("url:") + 4).trim();
|
|
|
|
|
}
|
|
|
|
|
if (httpParam.startsWith("method:")) {
|
|
|
|
|
method = httpParam.substring(httpParam.indexOf("method:") + 7).trim().toUpperCase();
|
|
|
|
|
}
|
|
|
|
|
if (httpParam.startsWith("data:")) {
|
|
|
|
|
data = httpParam.substring(httpParam.indexOf("data:") + 5).trim();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// param valid
|
|
|
|
|
if (url==null || url.trim().length()==0) {
|
|
|
|
|
XxlJobLogger.log("url["+ url +"] invalid.");
|
|
|
|
|
return ReturnT.FAIL;
|
|
|
|
|
}
|
|
|
|
|
if (method==null || !Arrays.asList("GET", "POST").contains(method)) {
|
|
|
|
|
XxlJobLogger.log("method["+ method +"] invalid.");
|
|
|
|
|
return ReturnT.FAIL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// request
|
|
|
|
@ -30,11 +57,11 @@ public class HttpJobHandler extends IJobHandler {
|
|
|
|
|
BufferedReader bufferedReader = null;
|
|
|
|
|
try {
|
|
|
|
|
// connection
|
|
|
|
|
URL realUrl = new URL(param);
|
|
|
|
|
URL realUrl = new URL(url);
|
|
|
|
|
connection = (HttpURLConnection) realUrl.openConnection();
|
|
|
|
|
|
|
|
|
|
// connection setting
|
|
|
|
|
connection.setRequestMethod("GET");
|
|
|
|
|
connection.setRequestMethod(method);
|
|
|
|
|
connection.setDoOutput(true);
|
|
|
|
|
connection.setDoInput(true);
|
|
|
|
|
connection.setUseCaches(false);
|
|
|
|
@ -47,12 +74,18 @@ public class HttpJobHandler extends IJobHandler {
|
|
|
|
|
// do connection
|
|
|
|
|
connection.connect();
|
|
|
|
|
|
|
|
|
|
//Map<String, List<String>> map = connection.getHeaderFields();
|
|
|
|
|
// data
|
|
|
|
|
if (data!=null && data.trim().length()>0) {
|
|
|
|
|
DataOutputStream dataOutputStream = new DataOutputStream(connection.getOutputStream());
|
|
|
|
|
dataOutputStream.write(data.getBytes("UTF-8"));
|
|
|
|
|
dataOutputStream.flush();
|
|
|
|
|
dataOutputStream.close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// valid StatusCode
|
|
|
|
|
int statusCode = connection.getResponseCode();
|
|
|
|
|
if (statusCode != 200) {
|
|
|
|
|
throw new RuntimeException("Http Request StatusCode("+ statusCode +") Invalid.");
|
|
|
|
|
throw new RuntimeException("Http Request StatusCode(" + statusCode + ") Invalid.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// result
|
|
|
|
@ -65,10 +98,10 @@ public class HttpJobHandler extends IJobHandler {
|
|
|
|
|
String responseMsg = result.toString();
|
|
|
|
|
|
|
|
|
|
XxlJobLogger.log(responseMsg);
|
|
|
|
|
return SUCCESS;
|
|
|
|
|
return ReturnT.SUCCESS;
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
XxlJobLogger.log(e);
|
|
|
|
|
return FAIL;
|
|
|
|
|
return ReturnT.FAIL;
|
|
|
|
|
} finally {
|
|
|
|
|
try {
|
|
|
|
|
if (bufferedReader != null) {
|
|
|
|
|