|
|
@ -5,9 +5,11 @@ import com.xxl.job.core.handler.IJobHandler;
|
|
|
|
import com.xxl.job.core.log.XxlJobLogger;
|
|
|
|
import com.xxl.job.core.log.XxlJobLogger;
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
|
|
|
|
import java.io.DataOutputStream;
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.net.URL;
|
|
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 跨平台Http任务
|
|
|
|
* 跨平台Http任务
|
|
|
@ -19,10 +21,35 @@ public class HttpJobHandler extends IJobHandler {
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public ReturnT<String> execute(String param) throws Exception {
|
|
|
|
public ReturnT<String> execute(String param) throws Exception {
|
|
|
|
|
|
|
|
|
|
|
|
// valid
|
|
|
|
// param parse
|
|
|
|
if (param==null || param.trim().length()==0) {
|
|
|
|
if (param==null || param.trim().length()==0) {
|
|
|
|
XxlJobLogger.log("URL Empty");
|
|
|
|
XxlJobLogger.log("param["+ param +"] invalid.");
|
|
|
|
return FAIL;
|
|
|
|
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
|
|
|
|
// request
|
|
|
@ -30,11 +57,11 @@ public class HttpJobHandler extends IJobHandler {
|
|
|
|
BufferedReader bufferedReader = null;
|
|
|
|
BufferedReader bufferedReader = null;
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
// connection
|
|
|
|
// connection
|
|
|
|
URL realUrl = new URL(param);
|
|
|
|
URL realUrl = new URL(url);
|
|
|
|
connection = (HttpURLConnection) realUrl.openConnection();
|
|
|
|
connection = (HttpURLConnection) realUrl.openConnection();
|
|
|
|
|
|
|
|
|
|
|
|
// connection setting
|
|
|
|
// connection setting
|
|
|
|
connection.setRequestMethod("GET");
|
|
|
|
connection.setRequestMethod(method);
|
|
|
|
connection.setDoOutput(true);
|
|
|
|
connection.setDoOutput(true);
|
|
|
|
connection.setDoInput(true);
|
|
|
|
connection.setDoInput(true);
|
|
|
|
connection.setUseCaches(false);
|
|
|
|
connection.setUseCaches(false);
|
|
|
@ -47,12 +74,18 @@ public class HttpJobHandler extends IJobHandler {
|
|
|
|
// do connection
|
|
|
|
// do connection
|
|
|
|
connection.connect();
|
|
|
|
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
|
|
|
|
// valid StatusCode
|
|
|
|
int statusCode = connection.getResponseCode();
|
|
|
|
int statusCode = connection.getResponseCode();
|
|
|
|
if (statusCode != 200) {
|
|
|
|
if (statusCode != 200) {
|
|
|
|
throw new RuntimeException("Http Request StatusCode("+ statusCode +") Invalid.");
|
|
|
|
throw new RuntimeException("Http Request StatusCode(" + statusCode + ") Invalid.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// result
|
|
|
|
// result
|
|
|
@ -65,10 +98,10 @@ public class HttpJobHandler extends IJobHandler {
|
|
|
|
String responseMsg = result.toString();
|
|
|
|
String responseMsg = result.toString();
|
|
|
|
|
|
|
|
|
|
|
|
XxlJobLogger.log(responseMsg);
|
|
|
|
XxlJobLogger.log(responseMsg);
|
|
|
|
return SUCCESS;
|
|
|
|
return ReturnT.SUCCESS;
|
|
|
|
} catch (Exception e) {
|
|
|
|
} catch (Exception e) {
|
|
|
|
XxlJobLogger.log(e);
|
|
|
|
XxlJobLogger.log(e);
|
|
|
|
return FAIL;
|
|
|
|
return ReturnT.FAIL;
|
|
|
|
} finally {
|
|
|
|
} finally {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
if (bufferedReader != null) {
|
|
|
|
if (bufferedReader != null) {
|
|
|
|