Browse Source

http返回处理

anderx 2 years ago
parent
commit
a22622c6d6

+ 2 - 3
src/main/java/com/goafanti/common/controller/PublicController.java

@@ -908,9 +908,8 @@ public class PublicController extends BaseController {
 		List<String> list =new ArrayList<>();
 		params.put("priors",list);
 		params.put("text",text);
-		JSONObject jsonObject = HttpUtils.httpPost("https://www.sciradar.com/gw/chat/chat/stream", params);
-		System.out.println(jsonObject.toJSONString());
-		return res.data(jsonObject.toJSONString());
+		String str = HttpUtils.httpPostToStr("https://www.sciradar.com/gw/chat/chat/stream", params);
+		return res.data(str);
 	}
 
 	/**

+ 38 - 1
src/main/java/com/goafanti/common/utils/HttpUtils.java

@@ -51,7 +51,7 @@ public class HttpUtils {
 			httpPost.addHeader("Content-type", "application/json");
 			httpPost.setHeader("Accept", "application/json");
 			httpPost.setEntity(new StringEntity(JSON.toJSONString(map), Charset.forName("UTF-8")));
-			httpPost.setHeader("Authorization","26a418106d92a32ea713f4fcec9e0ce6");
+
 			HttpResponse response = null;
 			try {
 				response = httpClient.execute(httpPost);
@@ -80,4 +80,41 @@ public class HttpUtils {
 
 
 
+	/**
+	 *
+	 * @param url 地址
+	 * @param map 参数
+	 * @return
+	 */
+	public static String httpPostToStr(String url,Map<String, Object>map){
+		HttpClient httpClient = HttpClientBuilder.create().build();
+		HttpPost httpPost = new HttpPost(url);
+		httpPost.addHeader("Content-type", "application/json");
+		httpPost.setHeader("Accept", "application/json");
+		httpPost.setEntity(new StringEntity(JSON.toJSONString(map), Charset.forName("UTF-8")));
+		httpPost.setHeader("Authorization","26a418106d92a32ea713f4fcec9e0ce6");
+		HttpResponse response = null;
+		try {
+			response = httpClient.execute(httpPost);
+			if (null == response || response.getStatusLine() == null) {
+				throw new Exception("Post Request For Url[{}] is not ok. Response is null");
+			} else if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
+				throw new Exception("Post Request For Url[{}] is not ok. Response Status Code is {"+response.getStatusLine().getStatusCode()+"}");
+			}
+		} catch (ClientProtocolException e) {
+			e.printStackTrace();
+		} catch (IOException e) {
+			e.printStackTrace();
+		} catch (Exception e) {
+			e.printStackTrace();
+		}
+		String resultString=null;
+		try {
+			resultString = EntityUtils.toString(response.getEntity(),"UTF-8");
+		} catch (ParseException | IOException e) {
+			e.printStackTrace();
+		}
+		return resultString;
+	}
+
 }