Commit 87e13686 by 111-PC\111

[+]http的put和delete方法添加

1 parent f9907ac6
...@@ -5,7 +5,6 @@ import cn.com.glsx.modules.model.param.Function; ...@@ -5,7 +5,6 @@ import cn.com.glsx.modules.model.param.Function;
import cn.com.glsx.modules.model.param.ResponseBody; import cn.com.glsx.modules.model.param.ResponseBody;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
......
...@@ -22,8 +22,10 @@ import org.apache.http.client.HttpClient; ...@@ -22,8 +22,10 @@ import org.apache.http.client.HttpClient;
import org.apache.http.client.HttpRequestRetryHandler; import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.client.config.RequestConfig; import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.protocol.HttpClientContext; import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.client.utils.HttpClientUtils; import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.config.Registry; import org.apache.http.config.Registry;
...@@ -175,7 +177,7 @@ public class HttpUtil { ...@@ -175,7 +177,7 @@ public class HttpUtil {
} }
/** /**
* 兼容旧版本工具类 * http发送post请求
* *
* @param url * @param url
* @return * @return
...@@ -234,6 +236,95 @@ public class HttpUtil { ...@@ -234,6 +236,95 @@ public class HttpUtil {
} }
} }
/**
* 作者:sanri <br/>
* 时间:2017-10-19下午2:25:40<br/>
* 功能:向指定 url 提交数据 <br/>
*
* @param url
* @param data
* @return
* @throws IOException
*/
public static String putData(String url, String data, Map<String, String> headerParams) throws IOException, HTTPException {
HttpClient httpClient = getHttpClient();
//请求头,请求体数据封装
HttpPut postMethod = new HttpPut(url);
postMethod.addHeader("Content-Type", "application/json");
if (!headerParams.isEmpty()) {
headerParams.forEach((key, value) -> {
postMethod.addHeader(key, value);
});
}
postMethod.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:56.0) Gecko/20100101 Firefox/56.0");
StringEntity dataEntity = new StringEntity(data, JSON_UTF8);
postMethod.setEntity(dataEntity);
HttpResponse response = null;
try {
//开始请求,记录请求数据和请求时间
response = httpClient.execute(postMethod);
//获取响应
HttpEntity entity = response.getEntity();
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode != 200) {
HTTPException exception = new HTTPException(statusCode);
logger.error("请求地址" + url + "出错,http 状态码为:" + statusCode, exception);
throw exception;
}
String retData = EntityUtils.toString(entity, JSON_UTF8.getCharset());
return retData;
} catch (ClientProtocolException e) {
throw new IllegalArgumentException("客户端协议错误 ,检查 url 配置 url: " + url, e);
} finally {
HttpClientUtils.closeQuietly(response);
}
}
/**
* 作者:sanri <br/>
* 时间:2017-10-19下午2:25:40<br/>
* 功能:向指定 url 提交数据 <br/>
*
* @param url
* @return
* @throws IOException
*/
public static String deleteData(String url, Map<String, String> headerParams) throws IOException, HTTPException {
HttpClient httpClient = getHttpClient();
//请求头,请求体数据封装
HttpDelete httpDelete = new HttpDelete(url);
httpDelete.addHeader("Content-Type", "application/json");
if (!headerParams.isEmpty()) {
headerParams.forEach((key, value) -> {
httpDelete.addHeader(key, value);
});
}
httpDelete.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:56.0) Gecko/20100101 Firefox/56.0");
HttpResponse response = null;
try {
//开始请求,记录请求数据和请求时间
response = httpClient.execute(httpDelete);
//获取响应
HttpEntity entity = response.getEntity();
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode != 200) {
HTTPException exception = new HTTPException(statusCode);
logger.error("请求地址" + url + "出错,http 状态码为:" + statusCode, exception);
throw exception;
}
String retData = EntityUtils.toString(entity, JSON_UTF8.getCharset());
return retData;
} catch (ClientProtocolException e) {
throw new IllegalArgumentException("客户端协议错误 ,检查 url 配置 url: " + url, e);
} finally {
HttpClientUtils.closeQuietly(response);
}
}
/** /**
* 作者:sanri <br/> * 作者:sanri <br/>
* 时间:2018-3-28下午8:57:50<br/> * 时间:2018-3-28下午8:57:50<br/>
...@@ -296,65 +387,54 @@ public class HttpUtil { ...@@ -296,65 +387,54 @@ public class HttpUtil {
return data; return data;
} }
/**
public static JSONObject getJSON(String url, Map<String, String> params, Charset charset, Map<String, String> headerParams) throws IOException { * http发送get请求
String retData = getData(url, params, charset, headerParams); *
* @param url
* @param params
* @param headerParams
* @return
* @throws IOException
*/
public static JSONObject sendGet(String url, Map<String, String> params, Map<String, String> headerParams) throws IOException {
String retData = getData(url, params, Consts.UTF_8, headerParams);
if (StringUtils.isNotBlank(retData)) { if (StringUtils.isNotBlank(retData)) {
return JSONObject.parseObject(retData); return JSONObject.parseObject(retData);
} }
return new JSONObject(); return new JSONObject();
} }
public static String postFormData(String url, Map<String, String> params, Charset charset) throws IOException { /**
CloseableHttpClient httpClient = getHttpClient(); * http的put方法
List<NameValuePair> nameValuePairs = transferParam(params); *
HttpPost postMethod = new HttpPost(url); * @param url 请求url
if (CollectionUtils.isNotEmpty(nameValuePairs)) { * @param json 请求body
HttpEntity entity = new UrlEncodedFormEntity(nameValuePairs, charset); * @param headerParams 请求参数
postMethod.setEntity(entity); * @return
} * @throws IOException
//TODO 测试用 */
postMethod.addHeader("Cookie", "JSESSIONID=node0oze509ig0of0vksk2dshvoi12.node0; fileDownload=true"); public static JSONObject sendPut(String url, String json, Map<String, String> headerParams) throws IOException {
postMethod.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"); String formData = putData(url, json, headerParams);
HttpResponse response = null; if (StringUtils.isNotBlank(formData)) {
try { return JSONObject.parseObject(formData);
response = httpClient.execute(postMethod);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode != 200) {
//解决地址重定向问题
if (statusCode == 302) {
Header firstHeader = response.getFirstHeader("location");
String location = firstHeader.getValue();
return postFormData(location, params, charset);
}
HTTPException exception = new HTTPException(statusCode);
logger.error("请求地址" + url + "出错,http 状态码为:" + statusCode, exception);
throw exception;
}
HttpEntity msgEntity = response.getEntity();
String message = EntityUtils.toString(msgEntity, charset);
return message;
} finally {
HttpClientUtils.closeQuietly(response);
} }
return new JSONObject();
} }
/** /**
* 作者:sanri <br/> * http发送delete请求
* 时间:2017-11-7下午6:24:30<br/>
* 功能:调用地址获取 json 数据 <br/>
* *
* @param url * @param url url
* @param params * @param headerParams 请求参数
* @return * @return
* @throws IllegalArgumentException
* @throws IOException * @throws IOException
*/ */
public static JSONObject sendGet(String url, Map<String, String> params, Map<String, String> headerParams) throws IOException { public static JSONObject sendDelete(String url,Map<String, String> headerParams) throws IOException {
return getJSON(url, params, Consts.UTF_8, headerParams); String retData = deleteData(url,headerParams);
if (StringUtils.isNotBlank(retData)) {
return JSONObject.parseObject(retData);
}
return new JSONObject();
} }
} }
\ No newline at end of file \ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!