您的位置:首页 > 理论基础 > 计算机网络

java发送http请求

2017-11-03 14:00 127 查看
第一步:添加依赖

<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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: