一、java代码
说明:
post请求入参为java对象。
含get和post两种方式的http请求。
import java.io.IOException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import com.fasterxml.jackson.databind.ObjectMapper;
public class HttpUtil {
public static void post(String url, Object requestObj)
throws ClientProtocolException, IOException {
// 创建一个默认的HttpClient
HttpClient httpclient = new DefaultHttpClient();
try {
// 以post方式请求
HttpPost httppost = new HttpPost(url);
String reqStr = new ObjectMapper().writeValueAsString(requestObj);
StringEntity params = new StringEntity(reqStr);
httppost.setEntity(params);
// 打印请求地址
System.out.println("executing request "
+ httppost.getRequestLine().getUri());
// 创建响应处理器处理服务器响应内容
ResponseHandler<String> responseHandler = new BasicResponseHandler();
// 执行请求并获取结果
String responseBody = httpclient.execute(httppost, responseHandler);
System.out.println("----------------------------------------");
System.out.println(responseBody);
System.out.println("----------------------------------------");
} finally {
// 当不再需要HttpClient实例时,关闭连接管理器以确保释放所有占用的系统资源
httpclient.getConnectionManager().shutdown();
}
}
//url:http://api.opentracker.net/api/views/view_pages.jsp?login=demo@opentracker.net&password=demo123&site=www.opentracker.net&filterByUrlTitle=*login*&offset=0&limit=10
public static void get(String url)
throws ClientProtocolException, IOException {
// 创建一个默认的HttpClient
HttpClient httpclient = new DefaultHttpClient();
try {
// 以get方式请求
HttpGet httpGet = new HttpGet(url);
// 打印请求地址
System.out.println("executing request "
+ httpGet.getRequestLine().getUri());
// 创建响应处理器处理服务器响应内容
ResponseHandler<String> responseHandler = new BasicResponseHandler();
// 执行请求并获取结果
String responseBody = httpclient.execute(httpGet, responseHandler);
System.out.println("----------------------------------------");
System.out.println(responseBody);
System.out.println("----------------------------------------");
} finally {
// 当不再需要HttpClient实例时,关闭连接管理器以确保释放所有占用的系统资源
httpclient.getConnectionManager().shutdown();
}
}
}
二、pom.xml文件如下
<!-- apache httpclient工具 -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.6</version>
</dependency>
<!-- apache httpclient工具end -->
<!-- json工具 -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.9</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.9</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.3.1</version>
</dependency>
<!-- json工具end -->
三、实现原理
1、利用HttpClient发送http请求;
2、利用jackson包将对象反序列化为字符串
引用jackson.databind包,利用注解@JsonSerialize、@JsonDeserialize 支持时间类型的格式化