自动执行某个POST请求任务
- 编写get请求方法获取任务id
- 将id封装成参数对象,编写post请求方法
- 通过循序将任务批量获取并执行
接收的对象
public class MyData {
private String chulirenName;
private String guidingjieshoushjian;
private String wanchengshijian;
private String nextTime;
private String chuliDeptName;
...
get/set ...
}
参数对象
public class ParamObj {
private String chuliState;
private String whsId;
private String id;
...
get/set ...
//POST请求传参时使用,需要将实际数值进行转码
@Override
public String toString() {
try {
return
"chuliState=" + URLEncoder.encode(chuliState,"utf-8") +
"&whsId=" + URLEncoder.encode(whsId,"utf-8") +
"&id=" + URLEncoder.encode(id,"utf-8") +
"&nextTime=" + URLEncoder.encode(nextTime,"utf-8") +
"&beizhu=" + URLEncoder.encode(beizhu,"utf-8") +
"&pingjiabeizhu=" + URLEncoder.encode(pingjiabeizhu,"utf-8") +
"&chuliren=" + URLEncoder.encode(chuliren,"utf-8") +
"&isfinish=" + URLEncoder.encode(isfinish,"utf-8")+
"&title="+ URLEncoder.encode(title,"utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return "";
}
}
执行类
package HTTP;
import com.beust.jcommander.internal.Nullable;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
public class HttpClientHelper {
public static ArrayList<String> list_id = new ArrayList<>();
public static ParamObj paramObj = new ParamObj("7", "000000", "572609", "2022-05-22 13:59:51", "已处理", "", "hcf", "false", "");
/**
* Http get请求
* 传入一个url字符串参数,解析响应数据获取id保存
*/
public static String doGet(String httpUrl) {
//链接
HttpURLConnection connection = null;
InputStream is = null;
BufferedReader br = null;
StringBuffer result = new StringBuffer();
try {
//创建连接
URL url = new URL(httpUrl);
connection = (HttpURLConnection) url.openConnection();
//设置请求方式
connection.setRequestMethod("GET");
//设置连接超时时间
connection.setReadTimeout(15000);
//开始连接
connection.connect();
//获取响应数据
if (connection.getResponseCode() == 200) {
//获取返回的数据
is = connection.getInputStream();
if (null != is) {
br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String temp = null;
while (null != (temp = br.readLine())) {
result.append(temp);
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != br) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != is) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//关闭远程连接
connection.disconnect();
}
//转换为json对象
JSONObject jsonObject = JSONObject.fromObject(result.toString());
//获取json对象data中的数据
JSONArray data = jsonObject.getJSONArray("data");
for (int i = 0; i < data.length(); i++) {
//将获取的单个json字符串翻译成JSONObject
JSONObject a = JSONObject.fromObject(data.get(i).toString());
//将json对象翻译成封装对象
MyData myData = (MyData) JSONObject.toBean(a, MyData.class);
//保存id字段
list_id.add(myData.getId());
}
//打印id列表
return list_id.toString();
}
/**
* Http post请求
*
* @param httpUrl 连接
* @param param 参数
* @return
*/
public static String doPost(String httpUrl, @Nullable ParamObj param) {
StringBuffer result = new StringBuffer();
//连接
HttpURLConnection connection = null;
DataOutputStream os = null;
InputStream is = null;
BufferedReader br = null;
try {
//创建连接对象
URL url = new URL(httpUrl);
System.out.println(url);
//创建连接
connection = (HttpURLConnection) url.openConnection();
//设置请求方法
connection.setRequestMethod("POST");
//设置连接超时时间
connection.setConnectTimeout(15000);
//设置读取超时时间
connection.setReadTimeout(15000);
//DoOutput设置是否向httpUrlConnection输出,DoInput设置是否从httpUrlConnection读入,此外发送post请求必须设置这两个
//设置是否可读取
connection.setDoOutput(true);
connection.setDoInput(true);
//设置通用的请求属性
connection.setRequestProperty("accept", "application/json");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Host", "task.51szg.com");
//拼装参数
if (null != param) {
//设置参数
os = new DataOutputStream(connection.getOutputStream());
String s = param.toString();
System.out.println(param.getId() + "任务处理开始");
os.writeBytes(s);
}
//设置权限
//设置请求头等
//开启连接
//connection.connect();
//读取响应
if (connection.getResponseCode() == 200) {
is = connection.getInputStream();
if (null != is) {
br = new BufferedReader(new InputStreamReader(is, "utf-8"));
String temp = null;
while (null != (temp = br.readLine())) {
result.append(temp);
result.append("\r\n");
}
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭连接
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//关闭连接
connection.disconnect();
}
return result.toString();
}
//输入页数获取每个任务的id存储在list中
public static void getMessage(int start, int end) throws UnsupportedEncodingException {
String title = "输入筛选的内容标题";
String encode = URLEncoder.encode(title, "utf-8");
String url = "http://www.***.com/work/hxWorktaskNode/list?chuliren=hcf&chuliState=2&pageSize=10&title=" + encode + "&page=";
for (int i = start; i <= end; i++) {
String s = doGet(url + i);
System.out.println(s);
try {
Thread.sleep(2500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
//发送post请求,传递参数完成任务
public static void runTask() {
for (String id : list_id) {
paramObj.setId(id);
String message = doPost("http://www.***.com/work/hxWorktaskNode/updateChuliState", paramObj);
System.out.println(message);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws UnsupportedEncodingException {
//获取第一页的信息,并执行任务
getMessage(1, 1);
runTask();
}
}