使用Apache HttpClient发送get、post请求

一、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 支持时间类型的格式化 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值