View Javadoc
1   package com.foxinmy.weixin4j.http.support.apache4;
2   
3   import java.io.IOException;
4   
5   import org.apache.http.client.methods.CloseableHttpResponse;
6   import org.apache.http.client.methods.HttpRequestBase;
7   import org.apache.http.impl.client.CloseableHttpClient;
8   
9   import com.foxinmy.weixin4j.http.HttpClientException;
10  import com.foxinmy.weixin4j.http.HttpRequest;
11  import com.foxinmy.weixin4j.http.HttpResponse;
12  
13  /**
14   * Requires Apache HttpComponents 4.3 or higher
15   * 
16   * @className HttpComponent4_2
17   * @author jinyu(foxinmy@gmail.com)
18   * @date 2015年8月18日
19   * @since JDK 1.6
20   * @see
21   */
22  public class HttpComponent4_2 extends HttpComponent4 {
23  
24  	private final CloseableHttpClient httpClient;
25  
26  	public HttpComponent4_2(CloseableHttpClient httpClient) {
27  		this.httpClient = httpClient;
28  	}
29  
30  	@Override
31  	public HttpResponse execute(HttpRequest request) throws HttpClientException {
32  		HttpResponse response = null;
33  		try {
34  			HttpRequestBase uriRequest = createRequest(request);
35  			CloseableHttpResponse httpResponse = httpClient.execute(uriRequest);
36  			response = new HttpComponent4_2Response(httpResponse,
37  					getContent(httpResponse));
38  			handleResponse(response);
39  		} catch (IOException e) {
40  			throw new HttpClientException("I/O error on "
41  					+ request.getMethod().name() + " request for \""
42  					+ request.getURI().toString(), e);
43  		} finally {
44  			if (response != null) {
45  				response.close();
46  			}
47  		}
48  		return response;
49  	}
50  }