java发送http请求
2017-11-03 14:00
127 查看
第一步:添加依赖
2、第二步:工具类
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.6</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.12</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.2</version> </dependency>
2、第二步:工具类
import com.alibaba.fastjson.JSONObject; import org.apache.http.HttpEntity; import org.apache.http.HttpStatus; 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.util.EntityUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; import java.net.URLDecoder; public class HttpRequestUtils { private static Logger logger = LoggerFactory.getLogger(HttpRequestUtils.class); //日志记录 /** * httpPost * * @param url 路径 * @param jsonParam 参数 * @return */ public static JSONObject httpPost(String url, JSONObject jsonParam) { return httpPost(url, jsonParam, false); } /** * post请求 * * @param url url地址 * @param jsonParam 参数 * @param noNeedResponse 不需要返回结果 * @return */ public static JSONObject httpPost(String url, JSONObject jsonParam, boolean noNeedResponse) { //post请求返回结果 CloseableHttpResponse response = null; try { CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost method = new HttpPost(url); if (null != jsonParam) { //解决中文乱码问题 StringEntity entity = new StringEntity(jsonParam.toString(), "UTF-8"); entity.setContentEncoding("UTF-8"); entity.setContentType("application/json"); method.setEntity(entity); } response = httpClient.execute(method); url = URLDecoder.decode(url, "UTF-8"); /**请求发送成功,并得到响应**/ if (response.getStatusLine().getStatusCode() == 200) { /**读取服务器返回过来的json字符串数据**/ HttpEntity entity = response.getEntity(); String strResult = EntityUtils.toString(entity); EntityUtils.consume(entity); if (noNeedResponse) { return null; } /**把json字符串转换成json对象**/ JSONObject jsonResult = JSONObject.parseObject(strResult); return jsonResult; } } catch (Exception e) { logger.error("post request fail:" + url, e); } finally { if (response != null) { try { response.close(); } catch (IOException e) { e.printStackTrace(); } } } return null; } /** * 发送get请求 * * @param url 路径 * @return */ public static JSONObject httpGet(String url) { //get请求返回结果 CloseableHttpResponse response = null; try { CloseableHttpClient client = HttpClients.createDefault(); //发送get请求 HttpGet request = new HttpGet(url); response = client.execute(request); /**请求发送成功,并得到响应**/ if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { /**读取服务器返回过来的json字符串数据**/ HttpEntity entity = response.getEntity(); String strResult = EntityUtils.toString(entity); EntityUtils.consume(entity); /**把json字符串转换成json对象**/ JSONObject jsonResult = JSONObject.parseObject(strResult); url = URLDecoder.decode(url, "UTF-8"); return jsonResult; } else { logger.error("get request fail:" + url); } } catch (Exception e) { logger.error("get request fail:" + url, e); } finally { if (response != null) { try { response.close(); } catch (Exception e) { e.printStackTrace(); } } } return null; } }
相关文章推荐
- java 发送http请求
- JAVA发送http请求
- java 常见几种发送http请求案例
- JAVA发送HTTP请求,并接受返回内容
- java发送http请求并获取状态码的简单实例
- java发送http请求
- java发送http请求
- java中发送http请求
- JAVA发送HTTP请求,并接受返回内容
- java发送http请求
- 【JAVA】通过URLConnection/HttpURLConnection发送HTTP请求的方法(一)
- java 作为客户端发送http请求。
- java使用post和get方式发送http请求
- java 发送HTTP请求工具
- Java发送HTTP请求和提取数据的Java代码
- Java 常用工具类(12) : java后台发送http请求
- JAVA发送HTTP请求,返回HTTP响应内容,实例及应用
- Java发送Http请求,解析html返回
- java发送http请求获取响应结果【工具包系列】
- java发送http请求并获取状态码