Browse Source

httpclient更新版本
加载物理世界bug修复

menglu 3 years ago
parent
commit
e0acac632c

+ 4 - 4
ibms-data-sdk/pom.xml

@@ -131,13 +131,13 @@
 
 		<dependency>
 			<groupId>org.apache.httpcomponents</groupId>
-			<artifactId>httpclient</artifactId>
-			<version>4.2.5</version>
+			<artifactId>httpcore</artifactId>
+			<version>4.4.14</version>
 		</dependency>
 		<dependency>
 			<groupId>org.apache.httpcomponents</groupId>
-			<artifactId>httpcore</artifactId>
-			<version>4.2.5</version>
+			<artifactId>httpclient</artifactId>
+			<version>4.5.13</version>
 		</dependency>
 
 		<dependency>

+ 44 - 7
ibms-data-sdk/src/main/java/com/persagy/ibms/data/sdk/util/HttpClientUtil.java

@@ -4,19 +4,32 @@ import java.io.ByteArrayOutputStream;
 import java.io.InputStream;
 
 import org.apache.http.HttpResponse;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.config.RequestConfig;
+import org.apache.http.client.methods.HttpGet;
 import org.apache.http.client.methods.HttpPost;
 import org.apache.http.entity.StringEntity;
-import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.impl.client.HttpClients;
 
 public class HttpClientUtil {
 
-	private static DefaultHttpClient client = new DefaultHttpClient();
+	private static final HttpClient client = HttpClients.createDefault();
 
-	public synchronized static String post(String post_url, String post_body) throws Exception {
-		HttpPost httpost = new HttpPost(post_url);
-		StringEntity entity = new StringEntity(post_body.toString(), "UTF-8");
-		entity.setContentType("application/json");
+	public synchronized static String post(String url, String content) throws Exception {
+		String result = post(url, content, 300000);
+		return result;
+	}
+
+	public synchronized static String post(String url, String content, Integer timeout) throws Exception {
+		HttpPost httpost = new HttpPost(url);
+		if (timeout != null) {
+			RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeout).setConnectionRequestTimeout(timeout)
+					.setSocketTimeout(timeout).build();
+			httpost.setConfig(requestConfig);
+		}
+		StringEntity entity = new StringEntity(content, "UTF-8");
 		httpost.setEntity(entity);
+		entity.setContentType("application/json");
 		HttpResponse httpResponse = client.execute(httpost);
 		InputStream is = httpResponse.getEntity().getContent();
 		ByteArrayOutputStream baos = new ByteArrayOutputStream();
@@ -24,9 +37,33 @@ public class HttpClientUtil {
 		while ((b = is.read()) != -1) {
 			baos.write(b);
 		}
-		String result = baos.toString("UTF-8");
 		baos.close();
 		is.close();
+
+		String result = baos.toString("UTF-8");
+		return result;
+	}
+
+	public synchronized static String get(String url, Integer timeout) throws Exception {
+		HttpGet httpget = new HttpGet(url);
+		if (timeout != null) {
+			RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeout).setConnectionRequestTimeout(timeout)
+					.setSocketTimeout(timeout).build();
+			httpget.setConfig(requestConfig);
+		}
+		// httpget.setHeader(new BasicHeader("Content-type", "text/plain;charset=utf-8"));
+		// httpget.setHeader(new BasicHeader("Content-type", "application/json;charset=utf-8"));
+		HttpResponse response = client.execute(httpget);
+		InputStream is = response.getEntity().getContent();
+		ByteArrayOutputStream os = new ByteArrayOutputStream();
+		int i = -1;
+		while ((i = is.read()) != -1) {
+			os.write(i);
+		}
+		is.close();
+		os.close();
+
+		String result = os.toString("UTF-8");
 		return result;
 	}
 }

+ 1 - 1
ibms-data-sdk/src/main/java/com/persagy/ibms/data/sdk/util/RWDLoadUtil.java

@@ -207,7 +207,7 @@ public class RWDLoadUtil {
 						String point = (String) value_primitive;
 						if (point != null) {
 							if (RWDLoadUtil.infoValue_is_point(point)) {
-								RepositoryBase.info2point.put(obj_id, new ConcurrentHashMap<String, String>());
+								RepositoryBase.info2point.putIfAbsent(obj_id, new ConcurrentHashMap<String, String>());
 								ConcurrentHashMap<String, String> pointMap = RepositoryBase.info2point.get(obj_id);
 								pointMap.putIfAbsent(Key, point);
 								// 表号功能号改为null