Browse Source

新增几个远程获取资源的方法

master
xyiege 4 years ago
parent
commit
1912cc26be
  1. 47
      src/main/java/cn/chjyj/szwh/utils/RequestUtils.java
  2. 5
      src/main/java/cn/chjyj/szwh/utils/SzwhApiUtils.java

47
src/main/java/cn/chjyj/szwh/utils/RequestUtils.java

@ -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

5
src/main/java/cn/chjyj/szwh/utils/SzwhApiUtils.java

@ -42,6 +42,10 @@ public class SzwhApiUtils {
return json;
}
/**
* 商品申请撤销
* @return
*/
public static JSONObject offGoods(){
String entHost= ProperUtils.getSzwhProp("ENTRUST_URL");//委托系统主机
String url=entHost+"/consign/exchange/v1/exchangeRevokeEntrust";
@ -51,7 +55,6 @@ public class SzwhApiUtils {
hmap.put("entrust_token",sign);
// 发送post请求
JSONObject json = RequestUtils.GetData(url,hmap);
return json;
}

Loading…
Cancel
Save