zoukankan      html  css  js  c++  java
  • 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("请求失败"); } } }

  • 相关阅读:
    组装树状结构文本框
    在jsp页面动态添加,删除文本框模块
    sql字段拆分 ,连表子查询获取值
    jsp页面技术总结
    C语言的基本数据类型
    如何学习一些需要算法的程序
    如何学习一门新的编程语言
    C++ 预处理器
    C++ 函数模板&类模板
    C++ 文件和流
  • 原文地址:https://www.cnblogs.com/HendSame-JMZ/p/6101945.html
Copyright © 2011-2022 走看看