|
|
|
@ -51,6 +51,7 @@ public class RequestUtils { |
|
|
|
inputStream.read(bs); |
|
|
|
//字节转为utf-8
|
|
|
|
String message = new String(bs, "UTF-8"); |
|
|
|
System.out.println("url ret string :"+message); |
|
|
|
jsonObject = JSONObject.parseObject(message); |
|
|
|
logger.info("respone message :"+message); |
|
|
|
}catch (IOException ex){ |
|
|
|
@ -95,6 +96,52 @@ public class RequestUtils { |
|
|
|
return jsonObject; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* post 方式提交数据 |
|
|
|
* @param url |
|
|
|
* @param params |
|
|
|
* @param header |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
public JSONObject postData(String url,String params,Map<String,Object> header){ |
|
|
|
JSONObject jsonObject = new JSONObject(); //返回的结果
|
|
|
|
|
|
|
|
CloseableHttpClient httpClient = HttpClients.createDefault(); |
|
|
|
HttpPost httpPost = new HttpPost(url); |
|
|
|
httpPost.setHeader("Accept","application/json"); |
|
|
|
httpPost.setHeader("Content-Type","application/json"); |
|
|
|
if(header!=null){ |
|
|
|
for(Map.Entry it:header.entrySet()){ |
|
|
|
httpPost.setHeader(it.getKey().toString(), |
|
|
|
it.getValue().toString()); |
|
|
|
} |
|
|
|
} |
|
|
|
// 设定字符集
|
|
|
|
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 = EntityUtils.toString(response.getEntity()); |
|
|
|
jsonObject = JSONObject.parseObject(outs); |
|
|
|
} |
|
|
|
}catch (IOException ex){ |
|
|
|
ex.printStackTrace(); |
|
|
|
}finally { |
|
|
|
try { |
|
|
|
httpClient.close(); |
|
|
|
}catch (IOException ex){ |
|
|
|
ex.printStackTrace(); |
|
|
|
} |
|
|
|
} |
|
|
|
return jsonObject; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 发送json 数据 |
|
|
|
* @param url |
|
|
|
|