HTTPClients的客户端执行请求,并获取响应,响应中解析具体的报文内容,最后返回请求内容将其转换成字符串
package com.fast.sky.util;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSONObject;
import com.fast.sky.domain.UserEntity;
/**
-
JDK version:1.8
-
extends:
-
implements:
*-------------------------------------------------------------- -
V1.0 创建 杨光 2019年6月7日
*--------------------------------------------------------------
*/
public class HttpRequestUtil {private static final Logger logger = LoggerFactory.getLogger(HttpRequestUtil.class);
private static CloseableHttpResponse response = null;
private static HttpEntity httpEntity = null;
private static StringEntity entity = null;/**
执行HTTP GET类型请求
-
@param requestURL/请求地址
-
@return 响应字符串
*/
public synchronized static String httpGet(String requestURL)
{
try
{if(null==requestURL || !requestURL.startsWith("http")) { logger.debug("The url is not exist ! Please check your Http request url ."); } //1.创建请求类型:GET HttpGet get = new HttpGet(requestURL); //为请求类型的头部添加信息 get.addHeader("Content-Type", "application/json;charset=utf-8"); //2.利用HTTPClients的客户端执行请求,并获取响应 response = HttpClients.custom().build().execute(get); int code = response.getStatusLine().getStatusCode();//获取执行之后的状态码,200执行正常 if(200 != code) { throw new RuntimeException("HTTP GET Request Failed with Error code : " + code); } //3.从响应中解析具体的报文内容 httpEntity = response.getEntity(); //4.最后返回请求内容将其转换成字符串 return EntityUtils.toString(httpEntity , "UTF-8");
}
catch (Exception e)
{
return “请求异常”;
}
}
/**
-
执行HTTP POST类型请求
-
@param requestURL/请求地址
-
@param data/附带参数
-
@return 响应字符串
*/
public synchronized static String httpPostRequest(String requestURL , String data)
{
try
{
if(null==requestURL || !requestURL.startsWith(“http”))
{
logger.error(“The url is not exist ! Please check your Http request url.”);
}HttpPost post = new HttpPost(requestURL.trim()); post.setHeader("Content-Type", "application/json;charset=utf-8"); entity = new StringEntity(data , ContentType.APPLICATION_JSON ); post.setEntity(entity); response = HttpClients.custom().build().execute(post); int code = response.getStatusLine().getStatusCode(); if(200 != code) { throw new RuntimeException("HTTP POST Request Failed with Error code : " + code); } httpEntity = response.getEntity(); return EntityUtils.toString(httpEntity , "UTF-8");
}
catch (Exception e)
{
return “请求异常”;
}
}
/**
-
执行HTTP DELETE类型请求
-
@param requestURL/请求地址
-
@return 响应字符串
*/
public synchronized static String httpDeleteRequest(String requestURL)
{
try
{
if(null == requestURL || !requestURL.startsWith(“http”))
{
logger.debug(“The url is not exist ! Please check your Http request url.”);
}HttpDelete delete = new HttpDelete(requestURL.trim()); delete.addHeader("Content-Type", "application/json;charset=utf-8"); response = HttpClients.custom().build().execute(delete); int code = response.getStatusLine().getStatusCode(); if(200 != code) { throw new RuntimeException("HTTP DELETE Request Failed with Error code : " + code); } httpEntity = response.getEntity(); return EntityUtils.toString(httpEntity , "UTF-8");
}
catch (Exception e)
{
return “请求异常”;
}
}
/**
-
执行HTTP PUT类型请求
-
@param requestURL/请求地址
-
@param data/附带参数
-
@return 响应字符串
*/
public synchronized static String httpPutRequest(String requestURL, String data)
{
try
{
if(null==requestURL || !requestURL.startsWith(“http”))
{
logger.debug(“The url is not exist ! Please check your Http request url.”);
}HttpPut put = new HttpPut(requestURL.trim()); put.setHeader("Content-Type", "application/json;charset=utf-8"); entity = new StringEntity(data , ContentType.APPLICATION_JSON ); put.setEntity(entity); response = HttpClients.custom().build().execute(put); int code = response.getStatusLine().getStatusCode(); if(200 != code) { throw new RuntimeException("HTTP PUT Request Failed with Error code : " + code); } httpEntity = response.getEntity(); return EntityUtils.toString(httpEntity , "UTF-8");
}
catch (Exception e)
{
return “请求异常”;
}
}
}
- HTTP一次请求/响应工作流程与报文解析
- httpclient发起https请求以及获取https返回内容
- HTTP报文内的HTTP信息之获取部分内容的范围请求
- HTTP代理实现请求报文的拦截与篡改7--将接收到的响应报文返回给客户端
- HTTP报文解析,请求报文头属性、响应码
- Gson利用泛型将Http请求返回的Json字符串快速解析模板
- http响应报文,如果响应的内容比较大,客户端怎么样判断接收完了呢?
- 速战速决 (6) - PHP: 获取 http 请求数据, 获取 get 数据 和 post 数据, json 字符串与对象之间的相互转换
- Js XMLHTTPRequest 实现GET请求提交和返回内容获取
- 从请求中获取xml内容并解析成map
- HTTP请求报文和HTTP响应报文 GET和Post的区别
- Android从 HttpResponse (或者InputStream) 获取字符串内容的代码
- Http请求报文和响应报文
- HTTP请求报文和响应报文
- c# .net mvc webapi Filters 筛选器中获取 请求参数和响应内容
- Java Json解析,Java Web Json解析,Java Web服务端获取Json数据,客户端通过HTTP获取Json数据
- 通俗易懂客户端与服务器端交互原理(HTTP数据请求与HTTP响应,包括Servlet部分)
- HTTP请求报文与响应报文
- HttpClient get和HttpClient Post请求的方式获取服务器的返回数据
- 005_Http之request获取客户端信息03-获取请求参数