使用httpclient实现http的post,get请求

httpclient是apache的一个高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议。我使用的是httpclient 4.2.5的版本,maven依赖如下:

<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpclient</artifactId>
			<version>4.2.5</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore -->
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpcore</artifactId>
			<version>4.2.5</version>
		</dependency>

然后增加两个pojo类,用于存放http请求与响应:

public class HttpRequest {
	private String url;
	private String body;
	
	public HttpRequest(String url, String body) {
		super();
		this.url = url;
		this.body = body;
	}
	
	public HttpRequest(String url) {
		super();
		this.url = url;
	}
	
	public HttpRequest() {
		// TODO Auto-generated constructor stub
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getBody() {
		return body;
	}

	public void setBody(String body) {
		this.body = body;
	}

	@Override
	public String toString() {
		return "HttpRequest [url=" + url + ", body=" + body + "]";
	}
	
}
public class HttpRequest {
	private String url;
	private String body;
	
	public HttpRequest(String url, String body) {
		super();
		this.url = url;
		this.body = body;
	}
	
	public HttpRequest(String url) {
		super();
		this.url = url;
	}
	
	public HttpRequest() {
		// TODO Auto-generated constructor stub
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getBody() {
		return body;
	}

	public void setBody(String body) {
		this.body = body;
	}

	@Override
	public String toString() {
		return "HttpRequest [url=" + url + ", body=" + body + "]";
	}
	
}

然后基于httpclient写一个http的支持get,post类,post方法是采用json格式传输

public class Http {

	public HttpResponse get(HttpRequest req) {
		HttpResponse response = new HttpResponse();
		
		try {
			HttpClient httpClient = new DefaultHttpClient();
			HttpGet httpGet = new HttpGet(req.getUrl());
			org.apache.http.HttpResponse httpResponse = httpClient.execute(httpGet);
			
			int statusCode = httpResponse.getStatusLine().getStatusCode();
			
			if(HttpStatus.SC_OK != statusCode) {
	        	
	        	throw new HttpException(statusCode);
	        } else {
	        	response.setStatusCode(statusCode);
	        	response.setBody(EntityUtils.toString(httpResponse.getEntity()));
	        }
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return response;
	}

	public HttpResponse post(HttpRequest req) {
		HttpResponse res = new HttpResponse();
		try {
			HttpClient httpClient = new DefaultHttpClient();
			httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 2000);
	        httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 2000);
	        
	        HttpPost post = new HttpPost(req.getUrl());
	        for (String key : GlobalConfig.HEAD_FINAL_MAP.keySet()) {
				post.setHeader(key, GlobalConfig.HEAD_FINAL_MAP.get(key));
			}
	        
	        StringEntity body = new StringEntity(req.getBody(), Charset.forName("UTF-8"));
	        body.setContentEncoding("UTF-8");
	        body.setContentType("application/json");
	        post.setEntity(body);
	        
	        org.apache.http.HttpResponse httpResponse = httpClient.execute(post);
	        
	        int statusCode = httpResponse.getStatusLine().getStatusCode();
	        
	        if(HttpStatus.SC_OK != statusCode) {
	        	
	        	throw new HttpException(statusCode);
	        } else {
	        	res.setStatusCode(statusCode);
	        	res.setBody(EntityUtils.toString(httpResponse.getEntity()));
	        }
	        
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return res;
	}


猜你喜欢

转载自blog.csdn.net/u013822349/article/details/80306707