You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
udi-wms-java/src/main/java/com/glxp/api/util/HttpClient.java

92 lines
3.6 KiB
Java

3 years ago
package com.glxp.api.util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.client.RestTemplate;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.Map;
@Slf4j
public class HttpClient {
// public static String uCloudPost(String url, Object object) {
// RestTemplate restTemplate = new RestTemplate();
// String json = JSONObject.toJSON(object).toString();
// log.info(url + "\n" + json);
// HttpHeaders headers = new HttpHeaders();
// headers.add("Content-Type", "application/json");
// HttpEntity<String> httpEntity = new HttpEntity<>(json, headers);
// log.info(httpEntity.toString());
// String response = restTemplate.postForObject(url, httpEntity, String.class);
// log.info(response);
// return response;
// }
/**
*
*
* @param url
* @return
*/
// public static String uCloudPost(String url, Object object, BasicThirdSysEntity basicThirdSysEntity) {
// RestTemplate restTemplate = new RestTemplate();
// String json = JSONObject.toJSON(object).toString();
// log.info(url + "\n" + json);
// HttpHeaders headers = new HttpHeaders();
// headers.add("Content-Type", "application/json");
// if (null != basicThirdSysEntity) {
// headers.add("api_key", basicThirdSysEntity.getApikey());
// headers.add("secret_key", basicThirdSysEntity.getSecretkey());
// }
// HttpEntity<String> httpEntity = new HttpEntity<>(json, headers);
// log.info(httpEntity.toString());
// return restTemplate.postForObject(url, httpEntity, String.class);
// }
public static String mipsGet(String url, Map params) {
StringBuffer stringBuffer = new StringBuffer(url);
if (params instanceof Map) {
Iterator iterator = ((Map) params).entrySet().iterator();
if (iterator.hasNext()) {
stringBuffer.append("?");
Object element;
while (iterator.hasNext()) {
element = iterator.next();
Map.Entry<String, Object> entry = (Map.Entry) element;
//过滤value为nullvalue为null时进行拼接字符串会变成 "null"字符串
if (entry.getValue() != null) {
String value = String.valueOf(((Map.Entry) element).getValue());
if (value.contains("#")) {
try {
String ps = URLEncoder.encode(value, "UTF-8");
((Map.Entry) element).setValue(ps);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
stringBuffer.append(element).append("&");
}
url = stringBuffer.substring(0, stringBuffer.length() - 1);
}
}
} else {
throw new RuntimeException("url请求:" + url + "请求参数有误不是map类型");
}
RestTemplate restTemplate = new RestTemplate();
String accessTokenRequestUrl = null;
try {
accessTokenRequestUrl = new URL(url).toString();
} catch (MalformedURLException e) {
e.printStackTrace();
}
log.info(accessTokenRequestUrl);
return restTemplate.getForObject(accessTokenRequestUrl, String.class);
}
}