Java发起Http请求
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.5</version>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
public class HttpUtils {
public String getUrl(String url, Map<String, String> paramsMap) throws Exception {
url = url.trim();
if (paramsMap != null && !paramsMap.isEmpty()) {
if (!url.contains("?")) {
url += "?";
}
StringBuilder urlBuilder = new StringBuilder(url);
for (String key : paramsMap.keySet()) {
urlBuilder.append(key).append("=").append(paramsMap.get(key)).append("&");
}
url = urlBuilder.toString();
}
if ("&".equals(url.substring(url.length() - 1))) {
return getUrl(url.substring(0, url.length() - 1));
}
return getUrl(url);
}
public String getUrl(String url) throws Exception {
GetMethod getMethod = new GetMethod(URIUtil.encodeQuery(url));
getMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
HttpClient client = new HttpClient();
int statusCode = client.executeMethod(getMethod);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: " + getMethod.getStatusLine());
}
String responseCharSet = getMethod.getResponseCharSet();
BufferedReader bufferedInputStream = new BufferedReader(new InputStreamReader(getMethod.getResponseBodyAsStream(), responseCharSet));
StringBuilder stringBuffer = new StringBuilder();
String str;
while ((str = bufferedInputStream.readLine()) != null) {
stringBuffer.append(str);
}
return stringBuffer.toString();
}
public String postUrl(String url, Map<String, String> paramsMap) throws IOException {
PostMethod postMethod = new PostMethod(url);
postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
if (paramsMap == null || paramsMap.isEmpty()) {
paramsMap = new HashMap<>();
}
for (String key : paramsMap.keySet()) {
postMethod.addParameter(key, MapUtils.getString(paramsMap, key));
}
HttpClient httpClient = new HttpClient();
int statusCode = httpClient.executeMethod(postMethod);
if (statusCode != HttpStatus.SC_OK) {
return null;
}
BufferedReader bufferedInputStream = new BufferedReader(new InputStreamReader(postMethod.getResponseBodyAsStream()));
StringBuilder stringBuffer = new StringBuilder();
String str;
while ((str = bufferedInputStream.readLine()) != null) {
stringBuffer.append(str);
}
return stringBuffer.toString();
}
public String postUrl(String url, String body) {
try {
PostMethod postMethod = new PostMethod(url);
RequestEntity requestEntity = new StringRequestEntity(body, "application/json", "UTF-8");
postMethod.setRequestEntity(requestEntity);
HttpClient httpClient = new HttpClient();
int statusCode = httpClient.executeMethod(postMethod);
if (statusCode != HttpStatus.SC_OK) {
return null;
}
BufferedReader bufferedInputStream = new BufferedReader(new InputStreamReader(postMethod.getResponseBodyAsStream()));
StringBuilder stringBuffer = new StringBuilder();
String str;
while ((str = bufferedInputStream.readLine()) != null) {
stringBuffer.append(str);
}
return stringBuffer.toString();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
@GetMapping("/http/{id}")
public void testGetHttp(@PathVariable("id") String id, @ModelAttribute TestGetBody testGetBody) {
}
HashMap<String, String> param = new HashMap<>();
param.put("name", "张三");
param.put("age", "23");
String url = httpUtils.getUrl("http://IP:端口/http/123456", param);
String url = httpUtils.getUrl("http://IP:端口/http/123456?name=张三&age=23");
@PostMapping("/http")
public void testPostHttp(@RequestBody TestGetBody testGetBody) {
}
String body = "{\"name\":\"张三\",\"age\":1}";
String url = httpUtils.postUrl("http://IP:端口/http", body);
@PostMapping("/http")
public void testPostHttp(@RequestParam("name") String name,
@RequestParam("age") Integer age) {
}
HashMap<String, String> param = new HashMap<>();
param.put("name", "张三");
param.put("age", "23");
String url = httpUtils.postUrl("http://IP:端口/http", param);