HttpComponents之httpclient
Posted Hendsame
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HttpComponents之httpclient相关的知识,希望对你有一定的参考价值。
HttpComponents源码目录:http://www.boyunjian.com/javasrc/org.apache.httpcomponents/httpclient/4.3.4/_/
package com.bbkj.utils; import com.alibaba.fastjson.JSON; import org.apache.http.HttpEntity; import org.apache.http.HttpHost; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.client.methods.RequestBuilder; import org.apache.http.conn.routing.HttpRoute; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import java.util.*; public class HttpUtils{ private static PoolingHttpClientConnectionManager connectionManager = null; private static HttpClientBuilder httpBulder = null; private static RequestConfig requestConfig = null; private static int MAXCONNECTION = 10; private static int DEFAULTMAXCONNECTION = 5; private static String IP = "abc.com"; private static int PORT = 80; static { //设置http的状态参数 requestConfig = RequestConfig.custom() .setSocketTimeout(5000) .setConnectTimeout(5000) .setConnectionRequestTimeout(5000) .build(); HttpHost target = new HttpHost(IP, PORT); connectionManager = new PoolingHttpClientConnectionManager(); connectionManager.setMaxTotal(MAXCONNECTION); connectionManager.setDefaultMaxPerRoute(DEFAULTMAXCONNECTION); connectionManager.setMaxPerRoute(new HttpRoute(target), 20); httpBulder = HttpClients.custom(); httpBulder.setConnectionManager(connectionManager); } public static CloseableHttpClient getConnection() { CloseableHttpClient httpClient = httpBulder.build(); httpClient = httpBulder.build(); return httpClient; } public static HttpUriRequest getRequestPost(Map<String, Object> map, String url) { List<NameValuePair> params = new ArrayList<NameValuePair>(); Set<Map.Entry<String, Object>> entrySet = map.entrySet(); HttpUriRequest reqMethod = null; StringEntity stringEntity = new StringEntity(JSON.toJSONString(map), ContentType.APPLICATION_JSON); reqMethod = RequestBuilder.post().setUri(url) .setEntity(stringEntity) .setConfig(requestConfig).build(); return reqMethod; } public static HttpUriRequest getRequestGet(Map<String, String> map, String url) { List<NameValuePair> params = new ArrayList<NameValuePair>(); Set<Map.Entry<String, String>> entrySet = map.entrySet(); for (Map.Entry<String, String> e : entrySet) { String name = e.getKey(); String value = e.getValue(); NameValuePair pair = new BasicNameValuePair(name, value); params.add(pair); } HttpUriRequest reqMethod = null; reqMethod = RequestBuilder.get().setUri(url) .addParameters(params.toArray(new BasicNameValuePair[params.size()])) .setConfig(requestConfig).build(); return reqMethod; } public static void main(String args[]) throws Exception { Map<String, Object> map = new HashMap<String, Object>(); // map.put("startindex", "1"); // map.put("size", "2"); map.put("sn","1"); map.put("index",1); map.put("heart_rate",1); map.put("resp_rate",1); map.put("leave_Bed",true); map.put("leave_bed_time",1); map.put("timestamp",1); map.put("bedNo","1"); /*** * String sn; //设备编号 Integer index; //序号 Integer heart_rate; //心率 Integer resp_rate; //呼吸/分钟 Boolean leave_Bed; //false:未离床,true:离床 Integer leave_bed_time; //离床时间(分钟) Long timestamp; String bedNo; */ HttpClient client = getConnection(); HttpUriRequest post = getRequestPost(map, "http://abc.com/bed/submit/data"); HttpResponse response = client.execute(post); System.out.println(response.getStatusLine().getStatusCode() ); if (response.getStatusLine().getStatusCode() == 200) { HttpEntity entity = response.getEntity(); String message = EntityUtils.toString(entity, "utf-8"); System.out.println(message); } else { System.out.println("请求失败"); } } }
以上是关于HttpComponents之httpclient的主要内容,如果未能解决你的问题,请参考以下文章
HttpComponents HttpClient连接池-结构
org.apache.httpcomponents.httpclient
org.apache.httpcomponents:httpclient 工具类