1 changed files with 91 additions and 0 deletions
@ -0,0 +1,91 @@ |
|||||
|
package cn.chjyj.szwh.utils; |
||||
|
|
||||
|
import com.alibaba.fastjson.JSONObject; |
||||
|
import org.apache.commons.logging.Log; |
||||
|
import org.apache.commons.logging.LogFactory; |
||||
|
import org.apache.http.StatusLine; |
||||
|
import org.apache.http.client.methods.CloseableHttpResponse; |
||||
|
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; |
||||
|
import java.net.HttpURLConnection; |
||||
|
import java.net.URL; |
||||
|
|
||||
|
/** |
||||
|
* 执行请求工具集 |
||||
|
*/ |
||||
|
public class RequestUtils { |
||||
|
private static Log logger = LogFactory.getLog(RequestUtils.class); |
||||
|
/** |
||||
|
* 利用GET方式获取uri数据 |
||||
|
* @param wxuri |
||||
|
* @return |
||||
|
*/ |
||||
|
public static JSONObject doGetUrlData(String wxuri){ |
||||
|
JSONObject jsonObject = new JSONObject(); |
||||
|
try { |
||||
|
URL url = new URL(wxuri); |
||||
|
HttpURLConnection httpcon = (HttpURLConnection) url.openConnection(); |
||||
|
httpcon.setRequestMethod("GET"); |
||||
|
httpcon.setDoOutput(true); |
||||
|
httpcon.setDoInput(true); |
||||
|
httpcon.connect(); |
||||
|
//获取返回的字符
|
||||
|
InputStream inputStream = httpcon.getInputStream(); |
||||
|
int size = inputStream.available(); |
||||
|
byte[] bs = new byte[size]; |
||||
|
inputStream.read(bs); |
||||
|
//字节转为utf-8
|
||||
|
String message = new String(bs, "UTF-8"); |
||||
|
jsonObject = JSONObject.parseObject(message); |
||||
|
} catch (IOException e) { |
||||
|
logger.error("请求错误:"+e.getMessage()); |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
return jsonObject; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 发送json 数据 |
||||
|
* @param url |
||||
|
* @param params |
||||
|
* @return |
||||
|
*/ |
||||
|
public static String doPost(String url,String params){ |
||||
|
CloseableHttpClient httpClient = HttpClients.createDefault(); |
||||
|
HttpPost httpPost = new HttpPost(url); |
||||
|
httpPost.setHeader("Accept","application/json"); |
||||
|
httpPost.setHeader("Content-Type","application/json"); |
||||
|
// 设定字符集
|
||||
|
String charset="UTF-8"; |
||||
|
StringEntity entity = new StringEntity(params,charset); |
||||
|
httpPost.setEntity(entity); |
||||
|
// 执行请求
|
||||
|
CloseableHttpResponse response = null; |
||||
|
try { |
||||
|
response = httpClient.execute(httpPost); |
||||
|
StatusLine httpStatus = response.getStatusLine(); |
||||
|
int status = httpStatus.getStatusCode(); |
||||
|
if(status== HttpStatus.OK.value()){ //状态码 为200
|
||||
|
//String outs = response.getEntity().toString();
|
||||
|
String outs = EntityUtils.toString(response.getEntity()); |
||||
|
return outs; |
||||
|
} |
||||
|
}catch (IOException ex){ |
||||
|
ex.printStackTrace(); |
||||
|
}finally { |
||||
|
try { |
||||
|
httpClient.close(); |
||||
|
}catch (IOException ex){ |
||||
|
ex.printStackTrace(); |
||||
|
} |
||||
|
} |
||||
|
return ""; |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue