新项目中使用新版的apache HttpClient调用http接口,贴上代码如下
import java.io.InputStream;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.codehaus.jackson.map.ObjectMapper;
@SuppressWarnings("unchecked")
private static Map<String, String> doGet(String address) {
Map<String, String> coordinate = new HashMap<String, String>();
if (StringUtils.isNotEmpty(address)) {
try {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope("test", 123), new UsernamePasswordCredentials("test", "test"));
CloseableHttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider)
.build();
Map<String, String> paramsMap = new LinkedHashMap<String, String>();
paramsMap.put("output", "json");
String paramsStr = BaiduSnCal.toQueryString(paramsMap);
String wholeStr = new String("" + paramsStr);
HttpGet httpGet = new HttpGet(wholeStr);
httpGet .addHeader("Referer", "");
// 设置代理
HttpHost proxy = new HttpHost("", 8080);
RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
httpGet.setConfig(config);
CloseableHttpResponse resp = httpClient.execute(httpGet);
InputStream in = resp.getEntity().getContent();
StringWriter w = new StringWriter();
IOUtils.copy(in, w, BaiduSnCal.CHARSET);
String result = w.toString();
System.out.println(result);
Map<String, Object> returnMap = mapper.readValue(result, Map.class);
System.out.println(((Map<String, Object>) returnMap.get("result")).get("location"));
} catch (Exception e) {
LOGGER.error("error : ", e);
}
}
return coordinate;
}
public static void main(String[] args) {
try {
getCoordinateByAddress("test");
} catch (Exception e) {
e.printStackTrace();
}
}
@SuppressWarnings("unchecked")
private static void doPost(String address) {
String url = "";
try {
CloseableHttpClient httpClient = HttpClients.custom().build();
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("Referer", "");
List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();
nvps.add(new BasicNameValuePair("", ""));
nvps.add(new BasicNameValuePair("output", "json"));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(nvps, ENCODING);
httpPost.setEntity(entity);
CloseableHttpResponse resp = httpClient.execute(httpPost);
InputStream in = resp.getEntity().getContent();
StringWriter w = new StringWriter();
IOUtils.copy(in, w, ENCODING);
String result = w.toString();
System.out.println(result);
Map<String, Object> returnMap = mapper.readValue(result, Map.class);
System.out.println(((Map<String, Object>)returnMap.get("result")).get("location"));
System.out.println(returnMap);
} catch (Exception e) {
e.printStackTrace();
}
}