资源简介
java http工具,包括get、post、json格式请求,使用httpclient
代码片段和文件信息
package com.xgdmsr.common.utils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
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.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
/**
* http工具类
*/
public class HttpUtil {
private static final Log logger = LogFactory.getLog(HttpUtil.class);
private static String ENCOCE_UTF_8 = “UTF-8“;
private static CloseableHttpClient client = null;
private HttpUtil() {
}
private static CloseableHttpClient getClientInstance() {
if (client == null) {
synchronized (HttpUtil.class) {
if (client == null) {
// 设置每个路由的最大并发连接数为20以提高性能,默认为2
client = HttpClientBuilder.create().setMaxConnPerRoute(20)
.build();
}
}
}
return client;
}
/**
* @param url
* @return
* @throws ClientProtocolException
* @throws IOException
*/
public static String httpGet(String url) throws ClientProtocolException IOException {
// 创建httpget
HttpGet httpGet = new HttpGet(url);
HttpResponse response = getClientInstance().execute(httpGet);
HttpEntity entity = response.getEntity();
return EntityUtils.toString(entity);
}
public static HttpEntity httpGetForEntity(String url) throws Exception {
HttpGet httpGet = new HttpGet(url);
HttpResponse response = getClientInstance().execute(httpGet);
return response.getEntity();
}
/**
* post请求
*
* @param url
* @param params
* @return
* @throws ClientProtocolException
* @throws IOException
*/
public static String httpPost(String url Map params) throws
相关资源
- JavaHTTP协议实现
- jsp 统计在线人数利用HttpSessionListene
- commons-codec-1.3.jar和commons-httpclient-3.0
- Java HttpClient 4.x Jar包
- httpcore-4.3.2.jar和httpmime-4.3.5.jar
- 基于http的Java爬虫爬取百度新闻
- Android通过http连接mysql
- JAVA 写的http服务器
- commons-httpclient-3.0
- HTTP服务端接口模拟工具-HttpServerMock
- apache httpclient jar包
- Android 7.0+抓包https突破ssl-pinning方案抓
- ApacheJMeter_http.jar与ApacheJMeter_core.jar.
- 模拟登录教务系统 抓取课表和成绩
- 模拟登录教务系统 抓取课表和成绩
- Android Http URL Connection获取数据并JSON解
- httpcomponents jar包
- httpclient-4.2.1.jar全部架包
- HttpClient配置SSL绕过https证书
- okhttp上传文件包含服务端java
- OkHttp3请求天气预报与Gson处理复杂JS
- Android网络请求
- MultipartEntityBuilder方法需要调用的jar包
- Android操作HTTP实现与服务器通信
- OkHttp上传文件并带进度条
- okHttp文件
- httpclient 相关的 3个jar包(commons-http
- httpclient 全部JAR包
- Android解析http常见数据格式的代码
- java httpclient https或http及文件中转上传
评论
共有 条评论