|
|
|
@ -5,14 +5,20 @@ import com.alibaba.fastjson2.JSONObject; |
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
import org.apache.commons.logging.Log; |
|
|
|
import org.apache.commons.logging.LogFactory; |
|
|
|
import org.apache.http.HttpEntity; |
|
|
|
import org.apache.http.HttpStatus; |
|
|
|
import org.apache.http.StatusLine; |
|
|
|
import org.apache.http.client.ClientProtocolException; |
|
|
|
import org.apache.http.client.HttpClient; |
|
|
|
import org.apache.http.client.config.RequestConfig; |
|
|
|
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.springframework.http.HttpStatus; |
|
|
|
|
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
import java.io.InputStream; |
|
|
|
@ -28,47 +34,45 @@ public class RequestUtils { |
|
|
|
private static Log logger = LogFactory.getLog(RequestUtils.class); |
|
|
|
|
|
|
|
/** |
|
|
|
* 能设置请求头的get方式 |
|
|
|
* @param surl |
|
|
|
* httpclient 方式获取信息 |
|
|
|
* @param url |
|
|
|
* @param map |
|
|
|
* @param wflag 是否写入json文件 |
|
|
|
* @param wflag |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
public static JSONObject GetData(String surl, Map<String,Object> map,boolean wflag){ |
|
|
|
public static JSONObject GetData(String url,Map<String,Object> map,boolean wflag){ |
|
|
|
JSONObject jsonObject = new JSONObject(); |
|
|
|
try{ |
|
|
|
URL url = new URL(surl); |
|
|
|
HttpURLConnection httpcon = (HttpURLConnection) url.openConnection(); |
|
|
|
// 遍历主机头信息
|
|
|
|
String result = StringUtils.EMPTY; |
|
|
|
CloseableHttpClient httpClient = HttpClients.createDefault(); |
|
|
|
HttpGet httpGet = new HttpGet(url); |
|
|
|
// 其他附加参数
|
|
|
|
for(Map.Entry item:map.entrySet()){ |
|
|
|
httpcon.addRequestProperty( |
|
|
|
item.getKey().toString(), |
|
|
|
item.getValue().toString() |
|
|
|
); |
|
|
|
} |
|
|
|
httpcon.setConnectTimeout(6000); |
|
|
|
httpcon.connect(); |
|
|
|
//获取返回的字符
|
|
|
|
InputStream inputStream = httpcon.getInputStream(); |
|
|
|
//请求结果写入
|
|
|
|
String filename =String.valueOf(Calendar.getInstance().getTimeInMillis()); |
|
|
|
if(wflag){ |
|
|
|
SzFileUtils.createFile(inputStream,filename); |
|
|
|
//字节转为utf-8
|
|
|
|
String message=SzFileUtils.readJson(filename); |
|
|
|
if(message!=null){ |
|
|
|
jsonObject = JSONObject.parseObject(message); |
|
|
|
logger.info("respone message :"+message); |
|
|
|
httpGet.addHeader(item.getKey().toString(), |
|
|
|
item.getValue().toString()); |
|
|
|
} |
|
|
|
logger.info("respone message is null"); |
|
|
|
} |
|
|
|
}catch (IOException ex){ |
|
|
|
// 罗列出明细错误
|
|
|
|
logger.error("请求错误:"+ex); |
|
|
|
ex.printStackTrace(); |
|
|
|
|
|
|
|
httpGet.setConfig(RequestConfig.custom().setConnectTimeout(1000).build()); |
|
|
|
try { |
|
|
|
CloseableHttpResponse response = httpClient.execute(httpGet); |
|
|
|
int status = response.getStatusLine().getStatusCode(); |
|
|
|
if (status == HttpStatus.SC_OK){ |
|
|
|
HttpEntity responseEntity = response.getEntity(); |
|
|
|
result = EntityUtils.toString(responseEntity, "UTF-8"); |
|
|
|
logger.info("api Request结果:"+result); |
|
|
|
jsonObject = JSONObject.parseObject(result); |
|
|
|
} |
|
|
|
// 转换为jsonobject
|
|
|
|
logger.info("httpGet状态:"+status); |
|
|
|
} catch (ClientProtocolException e) { |
|
|
|
logger.error("请求错误:"+e); |
|
|
|
e.printStackTrace(); |
|
|
|
} catch (IOException e) { |
|
|
|
logger.error("请求错误:"+e); |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
return jsonObject; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 利用GET方式获取uri数据 |
|
|
|
* @param requri 请求地址 |
|
|
|
@ -134,7 +138,7 @@ public class RequestUtils { |
|
|
|
response = httpClient.execute(httpPost); |
|
|
|
StatusLine httpStatus = response.getStatusLine(); |
|
|
|
int status = httpStatus.getStatusCode(); |
|
|
|
if(status== HttpStatus.OK.value()){ //状态码 为200
|
|
|
|
if(status== HttpStatus.SC_OK){ //状态码 为200
|
|
|
|
String outs = EntityUtils.toString(response.getEntity()); |
|
|
|
jsonObject = JSONObject.parseObject(outs); |
|
|
|
} |
|
|
|
@ -171,7 +175,7 @@ public class RequestUtils { |
|
|
|
response = httpClient.execute(httpPost); |
|
|
|
StatusLine httpStatus = response.getStatusLine(); |
|
|
|
int status = httpStatus.getStatusCode(); |
|
|
|
if(status== HttpStatus.OK.value()){ //状态码 为200
|
|
|
|
if(status== HttpStatus.SC_OK){ //状态码 为200
|
|
|
|
//String outs = response.getEntity().toString();
|
|
|
|
String outs = EntityUtils.toString(response.getEntity()); |
|
|
|
return outs; |
|
|
|
|