HttpClientUtil
来源:互联网 发布:seo 锚文本 编辑:程序博客网 时间:2024/06/08 00:17
//// Source code recreated from a .class file by IntelliJ IDEA// (powered by Fernflower decompiler)//package com.sprucetec.datacenter.util;import java.io.IOException;import java.io.UnsupportedEncodingException;import java.util.ArrayList;import java.util.Iterator;import java.util.Map;import java.util.Set;import org.apache.http.Consts;import org.apache.http.HttpEntity;import org.apache.http.client.config.RequestConfig;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.methods.HttpPost;import org.apache.http.entity.StringEntity;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.message.BasicNameValuePair;import org.apache.http.util.EntityUtils;import org.slf4j.Logger;import org.slf4j.LoggerFactory;public class HttpClientUtil { private static Logger log = LoggerFactory.getLogger(HttpClientUtil.class); public static final int DEFAULT_SOCKET_TIMEOUT = 5000; public static final int DEFAULT_CONNECT_TIMEOUT = 5000; public static final int DEFAULT_CONNECTION_REQUEST_TIMEOUT = 5000; private static CloseableHttpClient httpClient = HttpClients.createDefault(); public HttpClientUtil() { } public static String post(String url) { return post(url, (Map)null); } public static String post(String url, Map<String, String> param) { return post(url, param, RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000).setConnectionRequestTimeout(5000).build()); } public static String post(String url, Map<String, String> param, RequestConfig requestConfig) { HttpPost httpPost = new HttpPost(url); httpPost.setConfig(requestConfig); CloseableHttpResponse response = null; Object responseString = null; if(null != param && param.size() > 0) { Set e = param.keySet(); ArrayList nvps = new ArrayList(); Iterator e1 = e.iterator(); while(e1.hasNext()) { String key = (String)e1.next(); nvps.add(new BasicNameValuePair(key, (String)param.get(key))); } try { log.info("set utf-8 form entity to httppost"); httpPost.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8.name())); } catch (UnsupportedEncodingException var18) { throw new RuntimeException(var18.getMessage(), var18); } } try { response = httpClient.execute(httpPost); HttpEntity e2 = response.getEntity(); if(e2 != null) { String nvps1 = EntityUtils.toString(e2); return nvps1; } } catch (IOException var20) { throw new RuntimeException(var20.getMessage(), var20); } finally { try { if(response != null) { response.close(); } } catch (IOException var19) { log.warn("response is not closed"); throw new RuntimeException(var19.getMessage(), var19); } } return (String)responseString; } public static String postJson(String url) { return postJson(url, (String)null); } public static String postJson(String url, String jsonEntity) { return postJson(url, jsonEntity, RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000).setConnectionRequestTimeout(5000).build()); } public static String postJson(String url, String jsonEntity, RequestConfig requestConfig) { HttpPost httpPost = new HttpPost(url); httpPost.setConfig(requestConfig); httpPost.addHeader("Content-Type", "application/json;charset=utf-8"); httpPost.addHeader("Connection", "Keep-Alive"); httpPost.addHeader("user-agent", "MEICAI/WMC"); CloseableHttpResponse response = null; Object responseString = null; if(jsonEntity != null) { httpPost.setEntity(new StringEntity(jsonEntity, Consts.UTF_8.name())); } try { response = httpClient.execute(httpPost); HttpEntity e = response.getEntity(); if(e != null) { String var7 = EntityUtils.toString(e); return var7; } } catch (IOException var17) { throw new RuntimeException(var17.getMessage(), var17); } finally { try { if(response != null) { response.close(); } } catch (IOException var16) { log.warn("response is not closed"); throw new RuntimeException(var16.getMessage(), var16); } } return (String)responseString; } public static String get(String url) { String result = ""; try { HttpGet e = new HttpGet(url); CloseableHttpResponse httpResponse = null; httpResponse = httpClient.execute(e); try { HttpEntity entity = httpResponse.getEntity(); if(null != entity) { result = EntityUtils.toString(entity); } } finally { httpResponse.close(); } } catch (Exception var9) { var9.printStackTrace(); } return result; }}
0 0
- HttpClientUtil
- HttpClientUtil
- HttpClientUtil
- HttpClientUtil
- HttpClientUtil
- HttpClientUtil
- HttpClientUtil
- HttpClientUtil
- HttpClientUtil
- httpclientutil
- HttpClientUtil
- HttpClientUtil
- HttpClientUtil
- HttpClientUtil工具
- HttpClientUtil的使用
- HttpClientUtil工具类
- HttpClientUtil访问方式
- HttpClientUtil工具类
- java调用shell脚本并传递参数
- SQL2005 provider: 命名管道提供程序 error: 40 无法打开到 SQL Server 的连接【申明:来源于网络】
- PAT 乙级 1060. 爱丁顿数(25)
- DEDECMS中添加kindeditor编辑器【只读模式】【空白导航】
- Winform遍历窗口的所有控件(两种方式)
- HttpClientUtil
- CSS3变形
- SQL 编程 -- 打印直角三角形, while经典案例 ,case end 经典案例
- CXF系列一:创建一个简单的web service
- memcached对key和value的限制
- HttpUtil
- RequestMapper参数属性解析(转)
- 算法提高 排列数
- 【Spark】Spark2.0.2在Hadoop2.7.3上的安装 二(集群安装)