hz-zhhq-app-service/greenH5modul/.svn/pristine/08/0857d94e37fbdb40475a611e7ca...

207 lines
7.3 KiB
Plaintext
Raw Normal View History

2025-01-21 13:12:35 +08:00
package com.nationalelectirc.utils;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.EntityBuilder;
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.ContentType;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.sun.org.apache.regexp.internal.recompile;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import static org.apache.http.HttpStatus.SC_OK;
public final class HttpClient {
private static final RequestConfig REQUEST_CONFIG = RequestConfig.custom().setSocketTimeout(15000).setConnectTimeout(10000).build();
private static final RequestConfig REQUEST_CONFIG_SPECIAL_OVERTIME=RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000).build();
/**
* 采用Get方式发送请求获取响应数据
* @param url
* @return
*/
public static JSONObject httpGet(String url){
CloseableHttpClient client = HttpClientBuilder.create().build();
HttpGet httpGet = new HttpGet(url);
httpGet.setConfig(REQUEST_CONFIG);
try {
CloseableHttpResponse chr = client.execute(httpGet);
//int statusCode = chr.getStatusLine().getStatusCode();
/*if (SC_OK != statusCode) {
throw new RuntimeException(String.format("%s查询出现异常", url));
}*/
String entity = EntityUtils.toString(chr.getEntity(), StandardCharsets.UTF_8);
JSONObject object = new JSONObject(entity);
return object;
} catch (Exception e) {
throw new RuntimeException(String.format("%s", url) + "查询出现异常");
} finally {
try {
client.close();
} catch (IOException e) {
}
}
}
/**
* 采用Post方式发送请求获取响应数据
*
* @param url url地址
* @param param 参数值键值对的字符串
* @return
*/
public static String httpPost(String url, String param) {
CloseableHttpClient client = HttpClientBuilder.create().build();
try {
HttpPost post = new HttpPost(url);
EntityBuilder builder = EntityBuilder.create();
builder.setContentType(ContentType.APPLICATION_JSON);
builder.setText(param);
post.setEntity(builder.build());
CloseableHttpResponse response = client.execute(post);
int statusCode = response.getStatusLine().getStatusCode();
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity, StandardCharsets.UTF_8);
return data;
} catch(Exception e){
throw new RuntimeException(e.getMessage());
} finally {
try{
client.close();
}catch(Exception ex){ }
}
}
/**
* 采用Get方式发送请求获取响应数据,特殊的超时
* @param url
* @return
*/
public static JSONObject httpGetSpecialTimeOut(String url){
CloseableHttpClient client = HttpClientBuilder.create().build();
HttpGet httpGet = new HttpGet(url);
httpGet.setConfig(REQUEST_CONFIG_SPECIAL_OVERTIME);
try {
CloseableHttpResponse chr = client.execute(httpGet);
//int statusCode = chr.getStatusLine().getStatusCode();
/*if (SC_OK != statusCode) {
throw new RuntimeException(String.format("%s查询出现异常", url));
}*/
String entity = EntityUtils.toString(chr.getEntity(), StandardCharsets.UTF_8);
JSONObject object = new JSONObject(entity);
return object;
} catch (Exception e) {
JSONObject jsonObject=new JSONObject();
try {
jsonObject.put("getFail", true);
} catch (JSONException jsonParseException) {
// TODO Auto-generated catch block
jsonParseException.printStackTrace();
}
return jsonObject;
// throw new RuntimeException(String.format("%s", url) + "查询出现异常");
} finally {
try {
client.close();
} catch (IOException e) {
}
}
}
/**
* 采用Post方式发送请求获取响应数据,特殊的超时
* @param url
* @return
*/
public static JSONObject httpPostSpecialTimeOut(String url, String param){
CloseableHttpClient client = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost(url);
httpPost.setConfig(REQUEST_CONFIG_SPECIAL_OVERTIME);
try {
EntityBuilder builder = EntityBuilder.create();
builder.setContentType(ContentType.APPLICATION_JSON);
builder.setText(param);
httpPost.setEntity(builder.build());
CloseableHttpResponse chr = client.execute(httpPost);
//int statusCode = chr.getStatusLine().getStatusCode();
/*if (SC_OK != statusCode) {
throw new RuntimeException(String.format("%s查询出现异常", url));
}*/
String entity = EntityUtils.toString(chr.getEntity(), StandardCharsets.UTF_8);
JSONObject object = new JSONObject(entity);
return object;
} catch (Exception e) {
JSONObject jsonObject=new JSONObject();
try {
jsonObject.put("getFail", true);
} catch (JSONException jsonParseException) {
// TODO Auto-generated catch block
jsonParseException.printStackTrace();
}
return jsonObject;
// throw new RuntimeException(String.format("%s", url) + "查询出现异常");
} finally {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static JSONArray httpPostSpecialTimeOutToArray(String url, String param) throws Exception{
CloseableHttpClient client = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost(url);
httpPost.setConfig(REQUEST_CONFIG_SPECIAL_OVERTIME);
JSONArray object =new JSONArray();
try {
EntityBuilder builder = EntityBuilder.create();
builder.setContentType(ContentType.APPLICATION_JSON);
builder.setText(param);
httpPost.setEntity(builder.build());
CloseableHttpResponse chr = client.execute(httpPost);
//int statusCode = chr.getStatusLine().getStatusCode();
/*if (SC_OK != statusCode) {
throw new RuntimeException(String.format("%s查询出现异常", url));
}*/
String entity = EntityUtils.toString(chr.getEntity(), StandardCharsets.UTF_8);
object = JSON.parseArray(entity);
} catch (Exception e) {
e.printStackTrace();
throw new Exception("查询出现异常");
} finally {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return object;
}
}