示例http client代码 MyHttpUtils.java 最后更新时间: 2023年08月30日
对应的httpclient版本为 4.5.11。
本代码仅做示例用,不建议在正式的生产环境中使用。
package com.amap.wia.openapi.test.lib;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.*;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
public class MyHttpUtils {
private static boolean isDebugLogOpen = true;
public static String get(String url) {
HttpGet httpget = new HttpGet(url);
log("get() with url:" + url);
return doRequest(httpget);
}
protected static void println(String s) {
System.out.println(s);
}
public static void log(String msg) {
if (isDebugLogOpen) {
println(msg);
}
}
public static String attachParamsToUrl(String url, Map<String, Object> param) {
String paramString = "";
if (param == null || param.size() == 0) {
return url;
}
for (Map.Entry<String, Object> entry : param.entrySet()) {
String tmp = urlencode(entry.getKey()) + "=" + urlencode(entry.getValue().toString()) + "&";
paramString += tmp;
}
if (url.contains("?")) {
url = url + "&" + paramString;
} else {
url = url + "?" + paramString;
}
return url;
}
public static String urlencode(String value) {
try {
return URLEncoder.encode(value, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
public static String postFormUrlencoded(String httpUrl, Map<String, Object> params) {
log("postFormUrlencoded() with url:" + httpUrl);
log("post arguments:" + params);
HttpPost request = new HttpPost(httpUrl);
List<NameValuePair> list = new ArrayList<NameValuePair>();
if (params != null) {
for (Map.Entry<String, Object> item : params.entrySet()) {
list.add(new BasicNameValuePair(item.getKey(), item.getValue().toString()));
}
}
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(list, StandardCharsets.UTF_8);
request.setEntity(formEntity);
return doRequest(request);
}
public static String postApplicationJson(String httpUrl, String jsonString) {
log("postApplicationJson() with url:" + httpUrl);
log("post json string:" + jsonString);
HttpPost request = new HttpPost(httpUrl);
StringEntity entity = new StringEntity(jsonString, StandardCharsets.UTF_8);
entity.setContentType("application/json");
entity.setContentEncoding(StandardCharsets.UTF_8.toString());
request.setEntity(entity);
return doRequest(request);
}
protected static String doRequest(HttpRequestBase request) {
String result = "";
try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
try (CloseableHttpResponse response = httpClient.execute(request)) {
String printResult = "";
HttpEntity responseEntity = response.getEntity();
if (responseEntity != null) {
result = EntityUtils.toString(responseEntity, StandardCharsets.UTF_8);
printResult = "response result:" + result;
} else {
printResult = "response empty result!";
}
log(printResult);
}
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
}