模拟url请求
public static void main(String[] args) {
try {
URL url = null;
// SimpleDateFormat format= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// Date date=format.parse("2021-07-13 12:00:00");
// Date date2=format.parse("2021-08-13 12:00:00");
url = new URL("http://127.0.0.1:8084/xxxx");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// conn.setRequestProperty("User-Agent", "PostmanRuntime/7.28.3");
conn.setRequestProperty("Accept", "*/*");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Accept-Encoding", "gzip, deflate, br");
conn.setRequestProperty("Connection", "keep-alive");
conn.setRequestProperty("Authorization", "Bearer a");
conn.setRequestMethod("PUT");// 提交方法POST|GET
conn.setConnectTimeout(10000);// 设置连接超时时间,单位毫秒
conn.setReadTimeout(10000);// 设置读取数据超时时间,单位毫秒
//Get请求不需要DoOutPut
conn.setDoOutput(true);// 是否打开输出流 true|false
conn.setDoInput(true);// 是否打开输入流true|false
conn.setUseCaches(false);// 是否缓存true|false
conn.connect();// 打开连接端口
//这一块get不需要 post需要
// DataOutputStream out = new DataOutputStream(conn.getOutputStream());// 打开输出流往对端服务器写数据
// Map<String,Object> map=new HashMap<>();
// map.put("userNo","HR000131781");
//数据打包
JSONObject obj = new JSONObject();
obj.put("userNo", "HR000131781");
obj.put("receivedDate", "2021-07-19 11:09:42");
obj.put("receivedemoneyTotal", 1000.0);
obj.put("uuid", "3416650664259584");
String query = obj.toString();
OutputStream out = conn.getOutputStream();
out.write(query.getBytes("UTF-8"));
// out.writeBytes(content);// 写数据,也就是提交你的表单 name=xxx&pwd=xxx/**/
out.flush();// 刷新
out.close();// 关闭输出流*/
InputStream is = conn.getInputStream();
StringBuffer sb = new StringBuffer();
// 获取返回值
BufferedReader br = new BufferedReader(new InputStreamReader(is));
CloseableHttpClient httpClient = HttpClients.createDefault();
System.out.println(conn.getHeaderFields());
System.out.println(br.readLine());
} catch (Exception e) {
e.printStackTrace();
}
}
BASE64Encoder 加密
BASE64Encoder encoder = new BASE64Encoder();
String text = gatherSyncEntity.getGatherName() + ":" + gatherSyncEntity.getGatherPassword();
String encodedText = encoder.encode(text.getBytes("UTF-8"));