|
|
@@ -14,156 +14,11 @@ import java.util.Map;
|
|
|
|
|
|
public class HuaFangPriceUtil {
|
|
|
|
|
|
- private static final OkHttpClient HTTP_CLIENT = new OkHttpClient();
|
|
|
- private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
|
|
|
- private static final MediaType FORM = MediaType.parse("application/x-www-form-urlencoded; charset=utf-8");
|
|
|
|
|
|
private static final String userName = "001008";
|
|
|
|
|
|
private static final String password = "lt666888";
|
|
|
|
|
|
- /**
|
|
|
- * 通用HTTP GET请求方法
|
|
|
- *
|
|
|
- * @param url 请求地址
|
|
|
- * @param headers 请求头映射
|
|
|
- * @return 响应结果字符串
|
|
|
- */
|
|
|
- public static String sendGetRequest(String url, Map<String, String> headers) {
|
|
|
- Request.Builder builder = new Request.Builder()
|
|
|
- .header("Content-Type", "application/json")
|
|
|
- .url(url);
|
|
|
- // 添加请求头
|
|
|
- if (headers != null && !headers.isEmpty()) {
|
|
|
- for (Map.Entry<String, String> entry : headers.entrySet()) {
|
|
|
- builder.header(entry.getKey(), entry.getValue());
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- Request request = builder.build();
|
|
|
-
|
|
|
- try (Response response = HTTP_CLIENT.newCall(request).execute()) {
|
|
|
- if (!response.isSuccessful()) {
|
|
|
- throw new IOException("请求异常: " + response.code());
|
|
|
- }
|
|
|
- return response.body().string();
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- return null;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 通用HTTP GET请求方法(无请求头)
|
|
|
- *
|
|
|
- * @param url 请求地址
|
|
|
- * @return 响应结果字符串
|
|
|
- */
|
|
|
- public static String sendGetRequest(String url) {
|
|
|
- return sendGetRequest(url, null);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 通用HTTP POST请求方法(JSON参数)
|
|
|
- *
|
|
|
- * @param url 请求地址
|
|
|
- * @param jsonParam JSON参数
|
|
|
- * @param headers 请求头映射
|
|
|
- * @return 响应结果字符串
|
|
|
- */
|
|
|
- public static String sendPostRequest(String url, String jsonParam, Map<String, String> headers) {
|
|
|
- RequestBody body = RequestBody.create(JSON, jsonParam);
|
|
|
- Request.Builder builder = new Request.Builder()
|
|
|
- .header("Content-Type", "application/json")
|
|
|
- .url(url)
|
|
|
- .post(body);
|
|
|
-
|
|
|
- // 添加请求头
|
|
|
- if (headers != null && !headers.isEmpty()) {
|
|
|
- for (Map.Entry<String, String> entry : headers.entrySet()) {
|
|
|
- builder.header(entry.getKey(), entry.getValue());
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- Request request = builder.build();
|
|
|
-
|
|
|
- try (Response response = HTTP_CLIENT.newCall(request).execute()) {
|
|
|
- if (!response.isSuccessful()) {
|
|
|
- throw new IOException("请求异常: " + response.code());
|
|
|
- }
|
|
|
- return response.body().string();
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- return null;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 通用HTTP POST请求方法(JSON参数,无请求头)
|
|
|
- *
|
|
|
- * @param url 请求地址
|
|
|
- * @param jsonParam JSON参数
|
|
|
- * @return 响应结果字符串
|
|
|
- */
|
|
|
- public static String sendPostRequest(String url, String jsonParam) {
|
|
|
- return sendPostRequest(url, jsonParam, null);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 通用HTTP POST请求方法(表单参数)
|
|
|
- *
|
|
|
- * @param url 请求地址
|
|
|
- * @param formParams 表单参数映射
|
|
|
- * @param headers 请求头映射
|
|
|
- * @return 响应结果字符串
|
|
|
- */
|
|
|
- public static String sendPostFormRequest(String url, Map<String, String> formParams, Map<String, String> headers) {
|
|
|
- StringBuilder formBody = new StringBuilder();
|
|
|
- if (formParams != null && !formParams.isEmpty()) {
|
|
|
- for (Map.Entry<String, String> entry : formParams.entrySet()) {
|
|
|
- if (formBody.length() > 0) {
|
|
|
- formBody.append("&");
|
|
|
- }
|
|
|
- formBody.append(entry.getKey()).append("=").append(entry.getValue());
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- RequestBody body = RequestBody.create(FORM, formBody.toString());
|
|
|
- Request.Builder builder = new Request.Builder()
|
|
|
- .header("Content-Type", "application/json")
|
|
|
- .url(url)
|
|
|
- .post(body);
|
|
|
-
|
|
|
- // 添加请求头
|
|
|
- if (headers != null && !headers.isEmpty()) {
|
|
|
- for (Map.Entry<String, String> entry : headers.entrySet()) {
|
|
|
- builder.header(entry.getKey(), entry.getValue());
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- Request request = builder.build();
|
|
|
-
|
|
|
- try (Response response = HTTP_CLIENT.newCall(request).execute()) {
|
|
|
- if (!response.isSuccessful()) {
|
|
|
- throw new IOException("请求异常: " + response.code());
|
|
|
- }
|
|
|
- return response.body().string();
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- return null;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 通用HTTP POST请求方法(表单参数,无请求头)
|
|
|
- *
|
|
|
- * @param url 请求地址
|
|
|
- * @param formParams 表单参数映射
|
|
|
- * @return 响应结果字符串
|
|
|
- */
|
|
|
- public static String sendPostFormRequest(String url, Map<String, String> formParams) {
|
|
|
- return sendPostFormRequest(url, formParams, null);
|
|
|
- }
|
|
|
|
|
|
public static String getToken() {
|
|
|
String token = "";
|
|
|
@@ -175,7 +30,7 @@ public class HuaFangPriceUtil {
|
|
|
params.put("userName", userName);
|
|
|
params.put("password", password);
|
|
|
|
|
|
- String tokenString = sendPostRequest("http://ds.gw.chenghuiyin.com/ordering/api/User/GetToken", JSONObject.toJSONString(params));
|
|
|
+ String tokenString = HttpUtils.sendPostRequest("http://ds.gw.chenghuiyin.com/ordering/api/User/GetToken", JSONObject.toJSONString(params));
|
|
|
System.out.println("获取token返回:" + tokenString);
|
|
|
HuaFangTokenVo huaFangTokenVo = JSONObject.parseObject(tokenString, HuaFangTokenVo.class);
|
|
|
RedisUtil.set(templateName, huaFangTokenVo.getAccessToken(), huaFangTokenVo.getExpiresIn() - 50);
|
|
|
@@ -191,7 +46,7 @@ public class HuaFangPriceUtil {
|
|
|
public static HuaFangPriceResultVo getPrice(HuaFangPriceRequstVo params) {
|
|
|
Map<String, String> heards = new HashMap<>();
|
|
|
heards.put("Authorization", "Bearer " + getToken());
|
|
|
- String resultText = sendPostRequest("http://ds.quote.chenghuiyin.com/api/app/price/calc-price", JSONObject.toJSONString(params), heards);
|
|
|
+ String resultText = HttpUtils.sendPostRequest("http://ds.quote.chenghuiyin.com/api/app/price/calc-price", JSONObject.toJSONString(params), heards);
|
|
|
HuaFangPriceResultVo result = JSONObject.parseObject(resultText, HuaFangPriceResultVo.class);
|
|
|
return result;
|
|
|
}
|