package com.haier.camelot.cdk.wdcommon.util;
import lombok.extern.slf4j.Slf4j;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
/**
* <p>Description: [http请求工具类]</p>
* @version 1.0
*/
@Slf4j
public class HttpRequestUtil {
/**
* <p>Discription:[发doPost请求]</p>
* @param destUrl
* @param postData
* @return
* @throws Exception
* @author:[zh-fan]
* @update:[日期YYYY-MM-DD] [更改人姓名][变更描述]
*/
public static String send(String destUrl, String postData,String type) throws Exception {
long date1 = System.currentTimeMillis();
HttpURLConnection urlconn = null;
BufferedReader rd = null;
OutputStream out = null;
try{
String recString = "";
URL url = null;
url = new URL(destUrl);
urlconn = (HttpURLConnection) url.openConnection();
urlconn.setRequestProperty("content-type",type);
urlconn.setRequestMethod("POST");
urlconn.setDoInput(true);
urlconn.setDoOutput(true);
urlconn.setConnectTimeout(3000);
urlconn.setReadTimeout(10000);
out = urlconn.getOutputStream();
out.write(postData.getBytes("UTF-8"));
rd = new BufferedReader(new InputStreamReader(urlconn.getInputStream(), "UTF-8"));
StringBuffer sb = new StringBuffer();
int ch;
while ((ch = rd.read()) > -1) {
sb.append((char) ch);
}
recString = sb.toString();
return recString;
}catch (Exception e){
log.error("发送请求失败catch:" + destUrl ,e);
throw new Exception(destUrl + ":" + e.getMessage());
}finally {
if(out!=null){
out.flush();
out.close();
}
if(rd!=null){
rd.close();
}
if(urlconn!=null) {
urlconn.disconnect();
}
log.error("HttpRequestUtil-send-url【"+destUrl+"】 耗时:"+(System.currentTimeMillis()-date1));
}
}
public static String sendGet(String destUrl,String type) throws Exception {
long date1 = System.currentTimeMillis();
HttpURLConnection urlconn = null;
BufferedReader rd = null;
try{
String recString = "";
URL url = null;
url = new URL(destUrl);
urlconn = (HttpURLConnection) url.openConnection();
urlconn.setRequestProperty("content-type",type);
urlconn.setRequestMethod("GET");
urlconn.setDoInput(true);
urlconn.setDoOutput(true);
rd = new BufferedReader(new InputStreamReader(urlconn.getInputStream(), "UTF-8"));
StringBuffer sb = new StringBuffer();
int ch;
while ((ch = rd.read()) > -1) {
sb.append((char) ch);
}
recString = sb.toString();
return recString;
}catch (Exception e){
log.error("发送请求失败catch:" + destUrl ,e);
throw new Exception(destUrl + ":" + e.getMessage());
}finally {
if(rd!=null){
rd.close();
}
if(urlconn!=null) {
urlconn.disconnect();
}
log.error("HttpRequestUtil-sendGet-url【"+destUrl+"】 耗时:"+(System.currentTimeMillis()-date1));
}
}
public static String sendUrl(String destUrl,String type) throws Exception {
long date1 = System.currentTimeMillis();
HttpURLConnection urlconn = null;
BufferedReader rd = null;
try{
String recString = "";
URL url = null;
url = new URL(destUrl);
urlconn = (HttpURLConnection) url.openConnection();
urlconn.setRequestProperty("content-type",type);
urlconn.setRequestMethod("POST");
urlconn.setDoInput(true);
urlconn.setDoOutput(true);
urlconn.setConnectTimeout(3000);
urlconn.setReadTimeout(5000);
rd = new BufferedReader(new InputStreamReader(urlconn.getInputStream(), "UTF-8"));
StringBuffer sb = new StringBuffer();
int ch;
while ((ch = rd.read()) > -1) {
sb.append((char) ch);
}
recString = sb.toString();
return recString;
}catch (Exception e){
log.error("发送请求失败catch:" + destUrl ,e);
throw new Exception(destUrl + ":" + e.getMessage());
}finally {
if(rd!=null){
rd.close();
}
if(urlconn!=null) {
urlconn.disconnect();
}
log.error("HttpRequestUtil-sendUrl-url【"+destUrl+"】 耗时:"+(System.currentTimeMillis()-date1));
}
}
public static String sendUrlWait10(String destUrl,String type) throws Exception {
long date1 = System.currentTimeMillis();
HttpURLConnection urlconn = null;
BufferedReader rd = null;
try{
String recString = "";
URL url = null;
url = new URL(destUrl);
urlconn = (HttpURLConnection) url.openConnection();
urlconn.setRequestProperty("content-type",type);
urlconn.setRequestMethod("POST");
urlconn.setDoInput(true);
urlconn.setDoOutput(true);
urlconn.setConnectTimeout(3000);
urlconn.setReadTimeout(10000);
rd = new BufferedReader(new InputStreamReader(urlconn.getInputStream(), "UTF-8"));
StringBuffer sb = new StringBuffer();
int ch;
while ((ch = rd.read()) > -1) {
sb.append((char) ch);
}
recString = sb.toString();
return recString;
}catch (Exception e){
log.error("发送请求失败catch:" + destUrl ,e);
throw new Exception(destUrl + ":" + e.getMessage());
}finally {
if(rd!=null){
rd.close();
}
if(urlconn!=null) {
urlconn.disconnect();
}
log.error("HttpRequestUtil-sendUrlWait10-url【"+destUrl+"】 耗时:"+(System.currentTimeMillis()-date1));
}
}
/*
*设置30spost请求post超时时间,
*/
public static String sendUrlLongWait(String destUrl,String type) throws Exception {
long date1 = System.currentTimeMillis();
HttpURLConnection urlconn = null;
BufferedReader rd = null;
try{
String recString = "";
URL url = null;
url = new URL(destUrl);
urlconn = (HttpURLConnection) url.openConnection();
urlconn.setRequestProperty("content-type",type);
urlconn.setRequestMethod("POST");
urlconn.setDoInput(true);
urlconn.setDoOutput(true);
urlconn.setConnectTimeout(3000);
urlconn.setReadTimeout(30000);
rd = new BufferedReader(new InputStreamReader(urlconn.getInputStream(), "UTF-8"));
StringBuffer sb = new StringBuffer();
int ch;
while ((ch = rd.read()) > -1) {
sb.append((char) ch);
}
recString = sb.toString();
return recString;
}catch (Exception e){
log.error("发送请求失败catch:" + destUrl ,e);
throw new Exception(destUrl + ":" + e.getMessage());
}finally {
if(rd!=null){
rd.close();
}
if(urlconn!=null) {
urlconn.disconnect();
}
log.error("HttpRequestUtil-sendUrlLongWait-url【"+destUrl+"】 耗时:"+(System.currentTimeMillis()-date1));
}
}
public static String sendUrlFY(String destUrl,String type) throws Exception {
long date1 = System.currentTimeMillis();
HttpURLConnection urlconn = null;
BufferedReader rd = null;
try{
String recString = "";
URL url = null;
url = new URL(destUrl);
urlconn = (HttpURLConnection) url.openConnection();
urlconn.setRequestProperty("content-type",type);
urlconn.setRequestMethod("POST");
urlconn.setDoInput(true);
urlconn.setDoOutput(true);
urlconn.setConnectTimeout(3000);
urlconn.setReadTimeout(120000);
rd = new BufferedReader(new InputStreamReader(urlconn.getInputStream(), "UTF-8"));
StringBuffer sb = new StringBuffer();
int ch;
while ((ch = rd.read()) > -1) {
sb.append((char) ch);
}
recString = sb.toString();
return recString;
}catch (Exception e){
log.error("发送请求失败catch:" + destUrl ,e);
throw new Exception(destUrl + ":" + e.getMessage());
}finally {
if(rd!=null){
rd.close();
}
if(urlconn!=null) {
urlconn.disconnect();
}
log.error("HttpRequestUtil-sendUrlFY-url【"+destUrl+"】 耗时:"+(System.currentTimeMillis()-date1));
}
}
}
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
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.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import java.io.*;
import java.net.URI;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
@Slf4j
public class HttpClientUtil {
private HttpClientUtil() {
}
public static void main(String[] args) {
}
/**
* 封装HTTP POST方法
*
* @param
* @param
* @return
* @throws ClientProtocolException
* @throws IOException
*/
public static String post(String url, Map<String, String> paramMap) throws ClientProtocolException, IOException {
HttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
//设置请求和传输超时时间
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(60000).setConnectTimeout(60000).build();
httpPost.setConfig(requestConfig);
List<NameValuePair> formParams = setHttpParams(paramMap);
UrlEncodedFormEntity param = new UrlEncodedFormEntity(formParams, "UTF-8");
httpPost.setEntity(param);
HttpResponse response = httpClient.execute(httpPost);
log.info("************{}", response);
String httpEntityContent = getHttpEntityContent(response);
log.info("************{}", httpEntityContent);
httpPost.abort();
log.info("************{}", httpEntityContent);
return httpEntityContent;
}
/**
* 封装HTTP POST方法
*
* @param
* @param (如JSON串)
* @return
* @throws ClientProtocolException
* @throws IOException
*/
public static String post(String url, String data) throws ClientProtocolException, IOException {
HttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
//设置请求和传输超时时间
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(60000).setConnectTimeout(60000).build();
httpPost.setConfig(requestConfig);
httpPost.setHeader("Content-Type", "text/json; charset=utf-8");
httpPost.setEntity(new StringEntity(URLEncoder.encode(data, "UTF-8")));
HttpResponse response = httpClient.execute(httpPost);
String httpEntityContent = getHttpEntityContent(response);
httpPost.abort();
return httpEntityContent;
}
/**
* 封装HTTP GET方法
*
* @param
* @return
* @throws ClientProtocolException
* @throws IOException
*/
public static String get(String url) throws ClientProtocolException, IOException {
HttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet();
//设置请求和传输超时时间
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(60000).setConnectTimeout(60000).build();
httpGet.setConfig(requestConfig);
httpGet.setURI(URI.create(url));
HttpResponse response = httpClient.execute(httpGet);
String httpEntityContent = getHttpEntityContent(response);
httpGet.abort();
return httpEntityContent;
}
/**
* 封装HTTP GET方法
*
* @param
* @param
* @return
* @throws ClientProtocolException
* @throws IOException
*/
public static String get(String url, Map<String, String> paramMap) throws ClientProtocolException, IOException {
HttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet();
//设置请求和传输超时时间
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(60000).setConnectTimeout(60000).build();
httpGet.setConfig(requestConfig);
List<NameValuePair> formparams = setHttpParams(paramMap);
String param = URLEncodedUtils.format(formparams, "UTF-8");
httpGet.setURI(URI.create(url + "?" + param));
HttpResponse response = httpClient.execute(httpGet);
String httpEntityContent = getHttpEntityContent(response);
httpGet.abort();
return httpEntityContent;
}
/**
* 封装HTTP PUT方法
*
* @param
* @param
* @return
* @throws ClientProtocolException
* @throws IOException
*/
public static String put(String url, Map<String, String> paramMap) throws ClientProtocolException, IOException {
HttpClient httpClient = HttpClients.createDefault();
HttpPut httpPut = new HttpPut(url);
//设置请求和传输超时时间
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(60000).setConnectTimeout(60000).build();
httpPut.setConfig(requestConfig);
List<NameValuePair> formparams = setHttpParams(paramMap);
UrlEncodedFormEntity param = new UrlEncodedFormEntity(formparams, "UTF-8");
httpPut.setEntity(param);
HttpResponse response = httpClient.execute(httpPut);
String httpEntityContent = getHttpEntityContent(response);
httpPut.abort();
return httpEntityContent;
}
/**
* 封装HTTP DELETE方法
*
* @param
* @return
* @throws ClientProtocolException
* @throws IOException
*/
public static String delete(String url) throws ClientProtocolException, IOException {
HttpClient httpClient = HttpClients.createDefault();
HttpDelete httpDelete = new HttpDelete();
//设置请求和传输超时时间
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(60000).setConnectTimeout(60000).build();
httpDelete.setConfig(requestConfig);
httpDelete.setURI(URI.create(url));
HttpResponse response = httpClient.execute(httpDelete);
String httpEntityContent = getHttpEntityContent(response);
httpDelete.abort();
return httpEntityContent;
}
/**
* 封装HTTP DELETE方法
*
* @param
* @param
* @return
* @throws ClientProtocolException
* @throws IOException
*/
public static String delete(String url, Map<String, String> paramMap) throws ClientProtocolException, IOException {
HttpClient httpClient = HttpClients.createDefault();
HttpDelete httpDelete = new HttpDelete();
//设置请求和传输超时时间
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(60000).setConnectTimeout(60000).build();
httpDelete.setConfig(requestConfig);
List<NameValuePair> formparams = setHttpParams(paramMap);
String param = URLEncodedUtils.format(formparams, "UTF-8");
httpDelete.setURI(URI.create(url + "?" + param));
HttpResponse response = httpClient.execute(httpDelete);
String httpEntityContent = getHttpEntityContent(response);
httpDelete.abort();
return httpEntityContent;
}
/**
* 设置请求参数
*
* @param
* @return
*/
private static List<NameValuePair> setHttpParams(Map<String, String> paramMap) {
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
Set<Map.Entry<String, String>> set = paramMap.entrySet();
for (Map.Entry<String, String> entry : set) {
formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
return formparams;
}
/**
* 获得响应HTTP实体内容
*
* @param response
* @return
* @throws IOException
* @throws UnsupportedEncodingException
*/
private static String getHttpEntityContent(HttpResponse response) throws IOException, UnsupportedEncodingException {
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream is = entity.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String line = br.readLine();
StringBuilder sb = new StringBuilder();
while (line != null) {
sb.append(line + "\n");
line = br.readLine();
}
return sb.toString();
}
return "";
}
}
版权声明:本文为qq_42981242原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。