`

HttpClientUtil

阅读更多
;

import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
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.URIBuilder;
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;

public class HttpClientUtil {

	public static String doGet(String url, Map<String, String> param,String charSet) {

		CloseableHttpClient httpclient = HttpClients.createDefault();
		String resultString = "";
		CloseableHttpResponse response = null;
		try {
			// 创建uri
			URIBuilder builder = new URIBuilder(url);
			if (param != null) {
				for (String key : param.keySet()) {
					builder.addParameter(key, param.get(key));
				}
			}
			URI uri = builder.build();
			HttpGet httpGet = new HttpGet(uri);
			
			response = httpclient.execute(httpGet);
			if (response.getStatusLine().getStatusCode() == 200) {
				resultString = EntityUtils.toString(response.getEntity(), charSet);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (response != null)
					response.close();
				httpclient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return resultString;
	}

	public static String doGet(String url,String charSet) {
		return doGet(url, null);
	}

	public static String doPost(String url, Map<String, String> param,String charSet) {
		CloseableHttpClient httpClient = HttpClients.createDefault();
		CloseableHttpResponse response = null;
		String resultString = "";
		try {
			// 创建Http Post请求
			HttpPost httpPost = new HttpPost(url);
			// 创建参数列表
			if (param != null) {
				List<NameValuePair> paramList = new ArrayList<>();
				for (String key : param.keySet()) {
					paramList.add(new BasicNameValuePair(key, param.get(key)));
				}
				// 模拟表单
				UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList, "UTF-8");
				httpPost.setEntity(entity);
			}
			// 执行http请求
			response = httpClient.execute(httpPost);
			if (response.getStatusLine().getStatusCode() == 200) {
				resultString = EntityUtils.toString(response.getEntity(), charSet);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (response != null)
					response.close();
				httpClient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return resultString;
	}
	
	public static String doPostStr(String url, String reqStr,String charSet) {
		// 创建Httpclient对象
		CloseableHttpClient httpClient = HttpClients.createDefault();
		CloseableHttpResponse response = null;
		String resultString = "";
		try {
			HttpPost httpPost = new HttpPost(url);
			if (reqStr != null) {
				StringEntity entity = new StringEntity(reqStr);
				httpPost.setEntity(entity);
			}
			response = httpClient.execute(httpPost);
			if (response.getStatusLine().getStatusCode() == 200) {
				resultString = EntityUtils.toString(response.getEntity(), charSet);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (response != null)
					response.close();
				httpClient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return resultString;
	}

	public static String doPost(String url) {
		return doPost(url, null);
	}
}

 

分享到:
评论

相关推荐

    HttpClientUtil工具类 里面包含GET POST 及返回值处理

    - **实例化HttpClientUtil**:在代码中创建HttpClientUtil的实例。 - **调用方法**:根据需求,调用GET或POST方法,传入必要的参数(如URL、请求头、请求体等)。 - **处理返回值**:获取到的返回值通常是一个...

    HttpClientUtil工具类发送get和post请求,支持http和https,支持发送文件

    下面我们将详细讨论HttpClientUtil工具类如何实现这些功能。 首先,HttpClientUtil工具类通常会封装HttpClient的基本操作,以便于开发者在应用中便捷地调用。GET和POST请求是HTTP协议中最常见的两种请求方法。GET...

    httpClientUtil工具类

    httpClientUtil工具类

    http请求工具类HttpClientUtil,get,post请求(csdn)————程序.pdf

    1. **HttpClientUtil 类结构**: 类中定义了一个静态的日志器 LOGGER,用于记录执行过程中的信息,使用的是 SLF4J 日志框架。此外,创建了一个静态的 `CloseableHttpClient` 实例 `client`,这是 HttpClient 的核心...

    HttpClientUtil.java

    HttpClientUtil请求第三方接口,可以直接使用,post请求方式、get请求方式都有,直接粘贴复制就ok

    HttpClientUtil工具类,调用第三方接口

    该工具类是java 调用第三方接口时需要使用到的。HttpClientUtil 包含get和post方法。

    httpclientutil

    在"HttpClientUtil2-发送json返回json测试通过.java"这个文件中,我们可以预见到这是一个实现了发送JSON数据并接收JSON响应的示例。以下是一些关键知识点: 1. **Apache HttpClient 库**:HttpClient 是 Apache ...

    HttpClientUtil4.java

    HttpClientUtil4.java 工具类

    httpclientUtil

    1.基于HttpClient-4.4.1封装的一个工具类; 2.基于HttpAsycClient-4.1封装的异步HttpClient工具类; 3.javanet包下面是基于jdk自带的UrlConnection进行封装的。 前2个工具类支持插件式配置Header、插件式配置...

    httpClientUtil

    自己做项目中用到的,以后保存下来以防以后用到。多多积累。

    HttpClientUtil2.java

    HttpClient汇总工具类 HttpClient汇总工具类HttpClient汇总工具类HttpClient汇总工具类

    Java Http工具类HttpClientUtil

    多年积累,功能比较强大,可设置路由连接数,时间,请求类型包括get,post, 参数包括urlcode,map,json,xml。注释很清楚。

    基于jmeter+Java+HttpclientUtil实现的接口测试工具

    基于jmeter+Java+HttpclientUtil实现的接口测试工具,通过Excel表格进行维护接口相关参数信息,借助Jmeter工具通过java请求设计接口测试自动化测试用例。代码重写了JavaSampleClient类,在使用过程中取出了冗余的...

Global site tag (gtag.js) - Google Analytics