一、Resttemplete
resttemplete通用方法exchange
1.先在maven引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.70</version>
</dependency>
2.方法
public String getCookie() throws IOException {
Map<String,Object> paramMap = new HashMap<>();
paramMap.put("bodyKey","bodyValue");
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type","application/json");
headers.add("headerKey","headerValue");
String url = "www.abc.com";
String response = sendHttpPost(url,JSONObject.toJSONString(paramMap),headers);
return response;
}
private String sendHttpPost(String url,String param, HttpHeaders headers) throws IOException {
HttpEntity requestEntity = new HttpEntity(param,headers);
ResponseEntity<String> response = restTemplate.exchange(url,HttpMethod.POST,requestEntity, String.class);
List<String> response = response.getHeaders().get("set-cookie");
return response.getBody;
}
二、java原生的net
1.发送http请求
private String sendConnHttpPut(String url ,Map<String, Object> param) throws IOException {
String requestBody = JSONObject.toJSONString(param);
String sign = null;
StringBuilder httpsPost = new StringBuilder();
try {
URL url3 = new URL(url);
HttpURLConnection conn = (HttpURLConnection) url3.openConnection();
conn.setConnectTimeout(5000);
conn.setReadTimeout(5000);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json;charset=utf-8");
conn.setDoOutput(true);
conn.setDoInput(true);
OutputStream outputStream = conn.getOutputStream();
outputStream.write(requestBody.getBytes(StandardCharsets.UTF_8));
InputStream is = conn.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String str = "";
while ((str = br.readLine()) != null) {
httpsPost.append(str);
}
is.close();
conn.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
return httpsPost.toString();
}
三、apache.commons.httpclient
1.先在maven引入依赖
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
2.方法
public String getHttpCookie() throws IOException {
Map<String, Object> paramMap = new HashMap<>();
paramMap.put("params", "params");
String url = "www.abc.com";
return sendPost(JSONObject.toJSONString(paramMap), url);
}
public String sendPost(String params, String requestUrl) throws IOException {
byte[] requestBytes = params.getBytes(StandardCharsets.UTF_8);
HttpClient httpClient = new HttpClient();
PostMethod postMethod = new PostMethod(requestUrl);
postMethod.setRequestHeader("Content-Type", "application/json");
InputStream inputStream = new ByteArrayInputStream(requestBytes, 0,
requestBytes.length);
RequestEntity requestEntity = new InputStreamRequestEntity(inputStream, requestBytes.length, "application/json; charset=utf-8");
postMethod.setRequestEntity(requestEntity);
httpClient.executeMethod(postMethod);
StringBuilder response = new StringBuilder("");
BufferedReader br = null;
try {
InputStream is = putMethod.getResponseBodyAsStream();
br = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
String str = "";
while ((str = br.readLine()) != null) {
response.append(str);
}
} catch (Exception e) {
System.out.println(e);
} finally {
if (br != null) {
br.close();
}
}
return response.toString();
}