若依中提交 POST 类型的方法为 HttpUtils.sendPost(String url, String param),可以同时处理 httphttps 请求。

处理 JSON 格式数据只需要修改原请求的 contentType 类型值即可。

原来的请求参数:

URL realUrl = new URL(url);
URLConnection conn = realUrl.openConnection();
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64)");
conn.setRequestProperty("Accept-Charset", "utf-8");
conn.setRequestProperty("contentType", "utf-8");
conn.setDoOutput(true);
conn.setDoInput(true);

修改后:

URL realUrl = new URL(url);
URLConnection conn = realUrl.openConnection();
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64)");
conn.setRequestProperty("Accept-Charset", "utf-8");
conn.setRequestProperty("contentType", "application/json;charset=utf-8");
conn.setDoOutput(true);
conn.setDoInput(true);

需要注意的是,param 参数应该通过 JSON.toJSONString(paramMap) 转化,而非简单 toString()