|
| 1 | +/** |
| 2 | + * 创建时间:2016-2-23 |
| 3 | + */ |
| 4 | +package cn.aofeng.demo.httpclient; |
| 5 | + |
| 6 | +import java.io.IOException; |
| 7 | +import java.net.URISyntaxException; |
| 8 | + |
| 9 | +import org.apache.http.Header; |
| 10 | +import org.apache.http.HttpEntity; |
| 11 | +import org.apache.http.StatusLine; |
| 12 | +import org.apache.http.client.ClientProtocolException; |
| 13 | +import org.apache.http.client.methods.CloseableHttpResponse; |
| 14 | +import org.apache.http.client.methods.HttpGet; |
| 15 | +import org.apache.http.client.methods.HttpPost; |
| 16 | +import org.apache.http.impl.client.CloseableHttpClient; |
| 17 | +import org.apache.http.impl.client.HttpClients; |
| 18 | +import org.apache.http.util.EntityUtils; |
| 19 | +import org.apache.log4j.Logger; |
| 20 | + |
| 21 | +/** |
| 22 | + * HttpClient的基本操作。 |
| 23 | + * |
| 24 | + * @author <a href="mailto:[email protected]">聂勇</a> |
| 25 | + */ |
| 26 | +public class HttpClientBasic { |
| 27 | + |
| 28 | + private static Logger _logger = Logger.getLogger(HttpClientBasic.class); |
| 29 | + |
| 30 | + private static String _targetHost = "http://127.0.0.1:8888"; |
| 31 | + |
| 32 | + private static String _charset = "utf-8"; |
| 33 | + |
| 34 | + public void get() throws URISyntaxException, ClientProtocolException, IOException { |
| 35 | + CloseableHttpClient client = HttpClients.createDefault(); |
| 36 | + HttpGet get = new HttpGet(_targetHost+"/get"); |
| 37 | + CloseableHttpResponse response = client.execute(get); |
| 38 | + processResponse(response); |
| 39 | + } |
| 40 | + |
| 41 | + private void processResponse(CloseableHttpResponse response) |
| 42 | + throws UnsupportedOperationException, IOException { |
| 43 | + try { |
| 44 | + // 获取响应头 |
| 45 | + Header[] headers = response.getAllHeaders(); |
| 46 | + for (Header header : headers) { |
| 47 | + _logger.info(header.getName() + ":" + header.getValue()); |
| 48 | + } |
| 49 | + |
| 50 | + // 获取状态信息 |
| 51 | + StatusLine sl =response.getStatusLine(); |
| 52 | + _logger.info( String.format("ProtocolVersion:%s, StatusCode:%d, Desc:%s", |
| 53 | + sl.getProtocolVersion().toString(), sl.getStatusCode(), sl.getReasonPhrase()) ); |
| 54 | + |
| 55 | + // 获取响应内容 |
| 56 | + HttpEntity entity = response.getEntity(); |
| 57 | + _logger.info( String.format("ContentType:%s, Length:%d, Encoding:%s", |
| 58 | + null == entity.getContentType() ? "" : entity.getContentType().getValue(), |
| 59 | + entity.getContentLength(), |
| 60 | + null == entity.getContentEncoding() ? "" : entity.getContentEncoding().getValue()) ); |
| 61 | + _logger.info(EntityUtils.toString(entity, _charset)); |
| 62 | +// _logger.info( IOUtils.toString(entity.getContent(), _charset) ); // 大部分情况下效果与上行语句等同,但实现上的编码处理不同 |
| 63 | + } finally { |
| 64 | + response.close(); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + public void post() throws ClientProtocolException, IOException { |
| 69 | + CloseableHttpClient client = HttpClients.createDefault(); |
| 70 | + HttpPost post = new HttpPost(_targetHost+"/post"); |
| 71 | + post.addHeader("Cookie", "character=abcdefghijklmnopqrstuvwxyz; sign=abc-123-jkl-098"); |
| 72 | + CloseableHttpResponse response = client.execute(post); |
| 73 | + processResponse(response); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * @param args |
| 78 | + */ |
| 79 | + public static void main(String[] args) throws Exception { |
| 80 | + HttpClientBasic basic = new HttpClientBasic(); |
| 81 | + basic.get(); |
| 82 | + basic.post(); |
| 83 | + } |
| 84 | + |
| 85 | +} |
0 commit comments