/**
* 测试调用第三方接口
*/
@RequestMapping(value = "/test", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
@ResponseBody
public Response test(@RequestBody QueryMonthRentOrder query) {
Response response = new Response<>(ResponseCodeType.SUCCESS);
// 把对象转换成json字符串
Gson gson = new Gson();
String json = gson.toJson(query);
// 请求接口地址
String url = "https://blog.csdn.net/qq_36725282";
HttpClient httpclient = null;
PostMethod post = null;
GetMethod get = null;
try {
// 创建连接
httpclient = new HttpClient();
post = new PostMethod(url);
// 设置编码方式
post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
// 添加参数
post.setRequestBody(json);
// 设置表头
post.setRequestHeader("XXXXXX", "XXXXXX");
post.setRequestHeader("Content-Type", "application/json");
// 执行请求
httpclient.executeMethod(post);
// 接口返回信息
String info = new String(post.getResponseBody(), "UTF-8");
System.out.println("***************" + info);
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭连接,释放资源
post.releaseConnection();
((SimpleHttpConnectionManager) httpclient.getHttpConnectionManager()).shutdown();
}
response.setData("");
return response;
}
还有一种工具类的
package com.stars.park.util;
import java.io.IOException;
import java.io.InputStream;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
/**
* 测试httpclient 4.0 1. 重新设计了HttpClient 4.0 API架构,彻底从内部解决了所有 HttpClient 3.x 已知的架构缺陷代码。 2. HttpClient 4.0 提供了更简洁,更灵活,更明确的API。 3. HttpClient 4.0 引入了很多模块化的结构。 4.
* HttpClient 4.0性能方面得到了不小的提升,包括更少的内存使用,通过使用HttpCore模块更高效完成HTTP传输。 5. 通过使用 协议拦截器(protocol interceptors), HttpClient 4.0实现了 交叉HTTP(cross-cutting HTTP protocol)
* 协议 6. HttpClient 4.0增强了对连接的管理,更好的处理持久化连接,同时HttpClient 4.0还支持连接状态 7. HttpClient 4.0增加了插件式(可插拔的)的 重定向(redirect) 和 验证(authentication)处理。 8. HttpClient
* 4.0支持通过代理发送请求,或者通过一组代理发送请求。 9. 更灵活的SSL context 自定义功能在HttpClient 4.0中得以实现。 10. HttpClient 4.0减少了在省城HTTP请求 和 解析HTTP响应 过程中的垃圾信息。 11. HttpClient团队鼓励所有的项目升级成
* HttpClient 4.0
*
*
*/
public class HttpClientUtils {
// private HttpClient client;
private CloseableHttpClient client;
private RequestConfig requestConfig;
private StringBuffer url = new StringBuffer();
private String encoding = "UTF-8";
private List params = new ArrayList();
public HttpClientUtils(String url) {
this.url.append(url);
this.url.setLength(0);
this.url.append(url);
client = HttpClients.createDefault();
this.requestConfig = RequestConfig.custom().setConnectTimeout(5* 1000).setSocketTimeout(8 * 1000).build();
/*
* client = new DefaultHttpClient(); HttpParams params = client.getParams(); params.setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, encoding);
* params.setParameter(CoreProtocolPNames.HTTP_ELEMENT_CHARSET, encoding); params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION,
* HttpVersion.HTTP_1_1); HttpConnectionParams.setConnectionTimeout(params, 30 * 1000);// 链接时间 HttpConnectionParams.setSoTimeout(params, 2 * 60 *
* 1000);// 请求时间
*/
}
public String doPostWithNoParamsName(String content) {
String result = null;
try {
HttpResponse response = null;
HttpPost postMethod = new HttpPost(this.url.toString());
StringEntity se = new StringEntity(content, "utf-8");
se.setContentEncoding("utf-8");
se.setContentType("application/json");
postMethod.setEntity(se);
postMethod.setConfig(requestConfig);
// HttpClient client = new DefaultHttpClient();
response = client.execute(postMethod);
result = EntityUtils.toString(response.getEntity(), "utf-8");
} catch (Exception e) {
e.printStackTrace();
} finally {
close();
// client.getConnectionManager().shutdown();
}
return result;
}
@SuppressWarnings("deprecation")
public String doPost() {
String result = null;
try {
HttpPost post = new HttpPost(this.url.toString());
// 由抛异常改为catch,防止控制台输出连接失败信息
post.setConfig(requestConfig);
post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
result = getRespone(post);
} catch (Exception e) {
e.printStackTrace();
} finally {
close();
// client.getConnectionManager().shutdown();
}
return result;
}
@SuppressWarnings("deprecation")
public InputStream doPostForStream() {
InputStream result = null;
try {
HttpPost post = new HttpPost(this.url.toString());
// 由抛异常改为catch,防止控制台输出连接失败信息
post.setConfig(requestConfig);
post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
result = getResponeForStream(post);
} catch (Exception e) {
e.printStackTrace();
} finally {
close();
// client.getConnectionManager().shutdown();
}
return result;
}
@SuppressWarnings("deprecation")
public String doPostWithSetHeader(LinkedHashMap headers) {
//HashMap无序 TreeMap根据键排序,默认升序 LinkedHashMap遍历结果跟插入顺序一致
String result = null;
try {
HttpPost post = new HttpPost(this.url.toString());
for (String key : headers.keySet()) {
post.setHeader(key, headers.get(key));
}
post.setConfig(requestConfig);
post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
result = getRespone(post);
} catch (Exception e) {
e.printStackTrace();
} finally {
close();
// client.getConnectionManager().shutdown();
}
return result;
}
public String doPostWithNoParamsNameSetHeader(LinkedHashMap headers,String content) {
//HashMap无序 TreeMap根据键排序,默认升序 LinkedHashMap遍历结果跟插入顺序一致
String result = null;
try {
HttpPost post = new HttpPost(this.url.toString());
for (String key : headers.keySet()) {
post.setHeader(key, headers.get(key));
}
StringEntity se = new StringEntity(content, "utf-8");
se.setContentEncoding("utf-8");
se.setContentType("application/json");
post.setEntity(se);
post.setConfig(requestConfig);
result = getRespone(post);
} catch (Exception e) {
e.printStackTrace();
} finally {
close();
// client.getConnectionManager().shutdown();
}
return result;
}
public void setParams(String key, Object value) {
if (value != null) {
params.add(new BasicNameValuePair(key, value.toString()));
}
}
public void setInitUrl(String newUrl) {
//this.url.delete(0, url.length());//清空StringBuffer方法一
this.url.setLength(0);//清空StringBuffer方法二
url.append(newUrl);
}
@SuppressWarnings("deprecation")
public String doGet(){
String result = null;
try {
int count = 0;
for (NameValuePair nv : params) {
if (count == 0) {
url.append("?");
} else {
url.append("&");
}
String key = nv.getName();
String value = nv.getValue();
url.append(key);
url.append("=");
url.append(value);
count++;
}
//System.out.println(this.url.toString());
HttpGet get = new HttpGet(this.url.toString());
get.setConfig(requestConfig);
result = getRespone(get);
}catch (Exception e) {
e.printStackTrace();
}finally {
close();//当同一个客户的client需要依次访问多个url时则不允许close() --即注释掉close()
//client.getConnectionManager().shutdown();
}
return result;
}
public String getUrl() throws Exception {
int count = 0;
for (NameValuePair nv : params) {
if (count == 0) {
url.append("?");
} else {
url.append("&");
}
String key = nv.getName();
String value = nv.getValue();
url.append(key);
url.append("=");
url.append(value);
count++;
}
return this.url.toString();
}
private String getRespone(HttpUriRequest req) throws Exception {
String result = "";
// 查看默认request头部信息
// System.out.println("Accept-Charset:" +
// req.getFirstHeader("Accept-Charset"));
// 以下这条如果不加会发现无论你设置Accept-Charset为gbk还是utf-8,他都会默认返回gb2312(本例针对google.cn来说)
req.setHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.2)");
// 用逗号分隔显示可以同时接受多种编码
req.setHeader("Accept-Language", "zh-cn,zh;q=0.5");
req.setHeader("Accept-Charset", "GB2312,utf-8;q=0.7,*;q=0.7");
req.setHeader("Cache-Control","max-age=100000");
// 验证头部信息设置生效
// System.out.println("Accept-Charset:" +
// req.getFirstHeader("Accept-Charset").getValue());
HttpResponse response;
try {
response = client.execute(req);
HttpEntity entity = response.getEntity();
if (entity != null) {
result = EntityUtils.toString(entity, encoding);
// EntityUtils.consume(entity);
}
} catch (SocketTimeoutException ex) {
throw ex;
} catch (RuntimeException ex) {
throw ex;
} catch (Exception ex) {
throw ex;
}
return result;
}
@SuppressWarnings("unused")
private InputStream getResponeForStream(HttpUriRequest req) throws Exception {
InputStream result = null;
// 查看默认request头部信息
// System.out.println("Accept-Charset:" +
// req.getFirstHeader("Accept-Charset"));
// 以下这条如果不加会发现无论你设置Accept-Charset为gbk还是utf-8,他都会默认返回gb2312(本例针对google.cn来说)
req.setHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.2)");
// 用逗号分隔显示可以同时接受多种编码
req.setHeader("Accept-Language", "zh-cn,zh;q=0.5");
req.setHeader("Accept-Charset", "GB2312,utf-8;q=0.7,*;q=0.7");
req.setHeader("Cache-Control","max-age=100000");
// 验证头部信息设置生效
// System.out.println("Accept-Charset:" +
// req.getFirstHeader("Accept-Charset").getValue());
HttpResponse response;
try {
response = client.execute(req);
HttpEntity entity = response.getEntity();
result=entity.getContent();
} catch (SocketTimeoutException ex) {
throw ex;
} catch (RuntimeException ex) {
throw ex;
} catch (Exception ex) {
throw ex;
}
return result;
}
public void setEncoding(String encoding) {
this.encoding = encoding;
}
/**
* 关闭httpClient 请求
*/
public void close() {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
测试工具类
public static void main(String[] args) {
HttpClientUtils http = new HttpClientUtils("http://restapi.amap.com/v3/geocode/geo");
Map params = new HashMap<>();
params.put("key", "389880a06e3f893ea46036f030c94700");
params.put("s", "rsv3");
params.put("city", "35");
params.put("address", "范县希望中学");
/*http.setParams("key", "389880a06e3f893ea46036f030c94700");
http.setParams("s", "rsv3");
http.setParams("city", "35");
http.setParams("address", "范县希望中学");*/
for (String qqq : params.keySet()) {
http.setParams(qqq, params.get(qqq));
}
String doGet = http.doGet();
System.out.println(doGet);
}