import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
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.client.utils.HttpClientUtils;
import org.apache.http.client.utils.URIBuilder;
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;
public class HttpClientUtil {
public static String post(String url, List<NameValuePair> params, int retrys) {
CloseableHttpResponse response = null;
CloseableHttpClient httpclient = null;
try {
httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost(url);
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(6000).setConnectTimeout(3000).build();
httppost.setConfig(requestConfig);
httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
System.out.println("executing request " + httppost.getURI());
response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
if (entity != null) {
return EntityUtils.toString(entity);
} else {
return "";
}
} catch (Exception e) {
if (retrys > 0) {
return post(url, params, retrys - 1);
} else {
//TODO: add log
return "";
}
} finally {
HttpClientUtils.closeQuietly(response);
HttpClientUtils.closeQuietly(httpclient);
}
}
public static String get(String url, List<NameValuePair> params, int retrys) {
CloseableHttpResponse response = null;
CloseableHttpClient httpclient = null;
try {
httpclient = HttpClients.createDefault();
URIBuilder builder = new URIBuilder(url);
if (null != params && !params.isEmpty()) {
builder.addParameters(params);
}
HttpGet httpget = new HttpGet(builder.toString());
System.out.println("executing request " + httpget.getURI());
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(6000).setConnectTimeout(3000).build();
httpget.setConfig(requestConfig);
response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
if (entity != null) {
return EntityUtils.toString(entity);
} else {
return "";
}
} catch (Exception e) {
if (retrys > 0) {
return get(url, params, retrys - 1);
} else {
//TODO:add log
return "";
}
} finally {
HttpClientUtils.closeQuietly(response);
HttpClientUtils.closeQuietly(httpclient);
}
}
public static void main(String[] args) {
String url = "http://xxxxx/trace/v2/entity/list";
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("ak", "xxxxxxxx"));
params.add(new BasicNameValuePair("service_id", "xxxxxx"));
System.out.println(get(url, params, 3));
url = "http://xxxxx/trace/v2/entity/add";
params.add(new BasicNameValuePair("entity_name", "xx"));
System.out.println(post(url, params, 3));
}
}
httpclient版本:4.5.1
相关推荐
下面我们将详细讨论HttpClientUtil工具类如何实现这些功能。 首先,HttpClientUtil工具类通常会封装HttpClient的基本操作,以便于开发者在应用中便捷地调用。GET和POST请求是HTTP协议中最常见的两种请求方法。GET...
httpClientUtil工具类
1. **HttpClientUtil 类结构**: 类中定义了一个静态的日志器 LOGGER,用于记录执行过程中的信息,使用的是 SLF4J 日志框架。此外,创建了一个静态的 `CloseableHttpClient` 实例 `client`,这是 HttpClient 的核心...
HttpClientUtil请求第三方接口,可以直接使用,post请求方式、get请求方式都有,直接粘贴复制就ok
该工具类是java 调用第三方接口时需要使用到的。HttpClientUtil 包含get和post方法。
在"HttpClientUtil2-发送json返回json测试通过.java"这个文件中,我们可以预见到这是一个实现了发送JSON数据并接收JSON响应的示例。以下是一些关键知识点: 1. **Apache HttpClient 库**:HttpClient 是 Apache ...
HttpClientUtil4.java 工具类
- **实例化HttpClientUtil**:在代码中创建HttpClientUtil的实例。 - **调用方法**:根据需求,调用GET或POST方法,传入必要的参数(如URL、请求头、请求体等)。 - **处理返回值**:获取到的返回值通常是一个...
1.基于HttpClient-4.4.1封装的一个工具类; 2.基于HttpAsycClient-4.1封装的异步HttpClient工具类; 3.javanet包下面是基于jdk自带的UrlConnection进行封装的。 前2个工具类支持插件式配置Header、插件式配置...
自己做项目中用到的,以后保存下来以防以后用到。多多积累。
HttpClient汇总工具类 HttpClient汇总工具类HttpClient汇总工具类HttpClient汇总工具类
多年积累,功能比较强大,可设置路由连接数,时间,请求类型包括get,post, 参数包括urlcode,map,json,xml。注释很清楚。
基于jmeter+Java+HttpclientUtil实现的接口测试工具,通过Excel表格进行维护接口相关参数信息,借助Jmeter工具通过java请求设计接口测试自动化测试用例。代码重写了JavaSampleClient类,在使用过程中取出了冗余的...