1.请参数是json格式
首先导入pom
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.5</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.51</version>
</dependency>
2。工具代码
public class JsonPost {
public static String send(String url, JSONObject jsonObject, String encoding) throws ParseException, IOException {
String body = "";
CloseableHttpClient client = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
StringEntity s = new StringEntity(jsonObject.toString(), "utf-8");
s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
"application/json"));
httpPost.setEntity(s);
httpPost.setHeader("Content-type", "application/json");
httpPost.setHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
CloseableHttpResponse response = client.execute(httpPost);
HttpEntity entity = response.getEntity();
if (entity != null) {
body = EntityUtils.toString(entity, encoding);
}
EntityUtils.consume(entity);
response.close();
return body;
}
public static void main(String[] args) throws IOException {
HashMap<String, String> params = new HashMap<>();
params.put("date", "20200630");
params.put("typeId", "dsafa");
HashMap<String, Object> map = new HashMap<>();
map.put("key", "42423=q8");
map.put("id", "42342352");
map.put("pageNum", "0");
map.put("pageSize", "1000");
map.put("params", params);
JSONObject json = new JSONObject(map);
String url = "http://dc-o.web.leiniao.com/operation/open/request";
System.out.println(send(url, json, "UTF-8"));
}
}
2.请求参数为form-data格式
public class FormatPost {
public static String sendSmsFormByPost(String path, Map<String ,Object> params){
URL u = null;
HttpURLConnection con = null;
StringBuffer sb = new StringBuffer();
if (params != null) {
for (Map.Entry<String, Object> e : params.entrySet()) {
sb.append(e.getKey());
sb.append("=");
sb.append(e.getValue());
sb.append("&");
}
sb.substring(0, sb.length() - 1);
}
try {
u = new URL(path);
con = (HttpURLConnection) u.openConnection();
POST 只能为大写,严格限制,post会不识别
con.setRequestMethod("POST");
con.setDoOutput(true);
con.setDoInput(true);
con.setUseCaches(false);
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
OutputStreamWriter osw = new OutputStreamWriter(con.getOutputStream(), "UTF-8");
osw.write(sb.toString());
osw.flush();
osw.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (con != null) {
con.disconnect();
}
}
StringBuffer buffer = new StringBuffer();
try {
BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
String temp;
while ((temp = br.readLine()) != null) {
buffer.append(temp);
buffer.append("\n");
}
} catch (Exception e) {
e.printStackTrace();
}
return buffer.toString();
}
public static void main(String[] args) throws IOException {
HashMap<String, Object> map = new HashMap<>();
map.put("key", "rwrwffcs");
map.put("id", "424252");
map.put("pageNum", "0");
map.put("pageSize", "1000");
map.put("params", "aaa");
String url = "http://dc-o.web.leiniao.com/operation/open/request";
System.out.println(sendSmsFormByPost(url, map));
}
}