Commit 5f8e34ecc49e0fdb6e2a8d24b896f410f645b0e8
1 parent
2849a8e992
Exists in
master
and in
6 other branches
update code
Showing 4 changed files with 139 additions and 0 deletions
- platform-operate-api/pom.xml
- platform-operate-api/src/main/java/com/lyms/platform/operate/web/service/SyncDataTaskService.java
- platform-operate-api/src/main/java/com/lyms/platform/operate/web/utils/HttpClientUtil.java
- platform-operate-api/src/main/java/com/lyms/platform/operate/web/utils/SSLClient.java
platform-operate-api/pom.xml
View file @
5f8e34e
| ... | ... | @@ -12,6 +12,11 @@ |
| 12 | 12 | |
| 13 | 13 | <dependencies> |
| 14 | 14 | <dependency> |
| 15 | + <groupId>org.apache.httpcomponents</groupId> | |
| 16 | + <artifactId>httpclient</artifactId> | |
| 17 | + <version>4.5.2</version> | |
| 18 | + </dependency> | |
| 19 | + <dependency> | |
| 15 | 20 | <groupId>commons-httpclient</groupId> |
| 16 | 21 | <artifactId>commons-httpclient</artifactId> |
| 17 | 22 | </dependency> |
platform-operate-api/src/main/java/com/lyms/platform/operate/web/service/SyncDataTaskService.java
View file @
5f8e34e
| ... | ... | @@ -6,6 +6,8 @@ |
| 6 | 6 | import com.lyms.platform.common.pojo.UpdateMultiData; |
| 7 | 7 | import com.lyms.platform.common.utils.*; |
| 8 | 8 | import com.lyms.platform.operate.web.request.GuideQuery; |
| 9 | +import com.lyms.platform.operate.web.utils.HttpClientUtil; | |
| 10 | +import com.lyms.platform.operate.web.utils.SSLClient; | |
| 9 | 11 | import com.lyms.platform.permission.model.Users; |
| 10 | 12 | import com.lyms.platform.permission.model.UsersQuery; |
| 11 | 13 | import com.lyms.platform.permission.service.UsersService; |
| ... | ... | @@ -19,6 +21,12 @@ |
| 19 | 21 | import org.apache.commons.io.FileUtils; |
| 20 | 22 | import org.apache.commons.lang.*; |
| 21 | 23 | import org.apache.commons.lang.StringUtils; |
| 24 | +import org.apache.http.HttpEntity; | |
| 25 | +import org.apache.http.HttpResponse; | |
| 26 | +import org.apache.http.client.entity.UrlEncodedFormEntity; | |
| 27 | +import org.apache.http.client.methods.HttpPost; | |
| 28 | +import org.apache.http.message.BasicNameValuePair; | |
| 29 | +import org.apache.http.util.EntityUtils; | |
| 22 | 30 | import org.joda.time.DateTime; |
| 23 | 31 | import org.springframework.beans.factory.annotation.Autowired; |
| 24 | 32 | import org.springframework.stereotype.Service; |
| ... | ... | @@ -81,6 +89,8 @@ |
| 81 | 89 | } |
| 82 | 90 | } |
| 83 | 91 | |
| 92 | + | |
| 93 | + | |
| 84 | 94 | public void qhdfySyncData() { |
| 85 | 95 | try { |
| 86 | 96 | HttpClient client = new HttpClient(); |
| ... | ... | @@ -115,6 +125,35 @@ |
| 115 | 125 | } |
| 116 | 126 | } catch (Exception e) { |
| 117 | 127 | ExceptionUtils.catchException(e, "qhdfySyncData Error."); |
| 128 | + } | |
| 129 | + } | |
| 130 | + | |
| 131 | + /** | |
| 132 | + * 同步秦皇岛的数据到线上 | |
| 133 | + */ | |
| 134 | + public void qhdfySyncDataSSL() { | |
| 135 | + | |
| 136 | + try{ | |
| 137 | + StringBuffer ids = new StringBuffer(); | |
| 138 | + String json = HttpClientUtil.doPost("https://area-qhd-api.healthbaby.com.cn:18019/findSyncData", new HashMap<String, String>(), "utf-8"); | |
| 139 | + if (com.lyms.platform.common.utils.StringUtils.isNotEmpty(json)) | |
| 140 | + { | |
| 141 | + List<SyncDataModel> list = JsonUtil.toList(json, SyncDataModel.class); | |
| 142 | + for (SyncDataModel model : list) { | |
| 143 | + boolean boo = mongoSyncService.syncData(model.getAction(), model.getDataId(), model.getClassName(), model.getJsonData()); | |
| 144 | + if (boo) { | |
| 145 | + ids.append(model.getId()); | |
| 146 | + ids.append(","); | |
| 147 | + } | |
| 148 | + } | |
| 149 | + if (ids.length() > 0) { | |
| 150 | + Map<String,String> params = new HashMap<String, String>(); | |
| 151 | + params.put("ids", ids.toString()); | |
| 152 | + HttpClientUtil.doPost("https://area-qhd-api.healthbaby.com.cn:18019/updateSyncData", params, "utf-8"); | |
| 153 | + } | |
| 154 | + } | |
| 155 | + }catch(Exception ex){ | |
| 156 | + ExceptionUtils.catchException(ex, "qhdfySyncDataSSL Error."); | |
| 118 | 157 | } |
| 119 | 158 | } |
| 120 | 159 |
platform-operate-api/src/main/java/com/lyms/platform/operate/web/utils/HttpClientUtil.java
View file @
5f8e34e
| 1 | +package com.lyms.platform.operate.web.utils; | |
| 2 | + | |
| 3 | +/** | |
| 4 | + * Created by Administrator on 2017-01-18. | |
| 5 | + */ | |
| 6 | +import java.util.*; | |
| 7 | +import java.util.Map.Entry; | |
| 8 | +import org.apache.http.HttpEntity; | |
| 9 | +import org.apache.http.HttpResponse; | |
| 10 | +import org.apache.http.NameValuePair; | |
| 11 | +import org.apache.http.client.HttpClient; | |
| 12 | +import org.apache.http.client.entity.UrlEncodedFormEntity; | |
| 13 | +import org.apache.http.client.methods.HttpPost; | |
| 14 | +import org.apache.http.message.BasicNameValuePair; | |
| 15 | +import org.apache.http.util.EntityUtils; | |
| 16 | + | |
| 17 | +/** | |
| 18 | + * 利用HttpClient进行post请求的工具类 | |
| 19 | + */ | |
| 20 | +public class HttpClientUtil { | |
| 21 | + public static String doPost(String url,Map<String,String> map,String charset){ | |
| 22 | + HttpClient httpClient = null; | |
| 23 | + HttpPost httpPost = null; | |
| 24 | + String result = null; | |
| 25 | + try{ | |
| 26 | + httpClient = new SSLClient(); | |
| 27 | + httpPost = new HttpPost(url); | |
| 28 | + //设置参数 | |
| 29 | + List<NameValuePair> list = new ArrayList<NameValuePair>(); | |
| 30 | + Iterator iterator = map.entrySet().iterator(); | |
| 31 | + while(iterator.hasNext()){ | |
| 32 | + Entry<String,String> elem = (Entry<String, String>) iterator.next(); | |
| 33 | + list.add(new BasicNameValuePair(elem.getKey(),elem.getValue())); | |
| 34 | + } | |
| 35 | + if(list.size() > 0){ | |
| 36 | + UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,charset); | |
| 37 | + httpPost.setEntity(entity); | |
| 38 | + } | |
| 39 | + HttpResponse response = httpClient.execute(httpPost); | |
| 40 | + if(response != null){ | |
| 41 | + HttpEntity resEntity = response.getEntity(); | |
| 42 | + if(resEntity != null){ | |
| 43 | + result = EntityUtils.toString(resEntity,charset); | |
| 44 | + } | |
| 45 | + } | |
| 46 | + }catch(Exception ex){ | |
| 47 | + ex.printStackTrace(); | |
| 48 | + } | |
| 49 | + return result; | |
| 50 | + } | |
| 51 | + | |
| 52 | +// public static void main(String[] args) throws Exception { | |
| 53 | +// | |
| 54 | +// String s = doPost("https://area-qhd-api.healthbaby.com.cn:18019/findSyncData", new HashMap<String, String>(), "utf-8"); | |
| 55 | +// System.out.println(s); | |
| 56 | +// } | |
| 57 | +} |
platform-operate-api/src/main/java/com/lyms/platform/operate/web/utils/SSLClient.java
View file @
5f8e34e
| 1 | +package com.lyms.platform.operate.web.utils; | |
| 2 | + | |
| 3 | +import java.security.cert.CertificateException; | |
| 4 | +import java.security.cert.X509Certificate; | |
| 5 | +import javax.net.ssl.SSLContext; | |
| 6 | +import javax.net.ssl.TrustManager; | |
| 7 | +import javax.net.ssl.X509TrustManager; | |
| 8 | +import org.apache.http.conn.ClientConnectionManager; | |
| 9 | +import org.apache.http.conn.scheme.Scheme; | |
| 10 | +import org.apache.http.conn.scheme.SchemeRegistry; | |
| 11 | +import org.apache.http.conn.ssl.SSLSocketFactory; | |
| 12 | +import org.apache.http.impl.client.DefaultHttpClient; | |
| 13 | +//用于进行Https请求的HttpClient | |
| 14 | +public class SSLClient extends DefaultHttpClient{ | |
| 15 | + public SSLClient() throws Exception{ | |
| 16 | + super(); | |
| 17 | + SSLContext ctx = SSLContext.getInstance("TLS"); | |
| 18 | + X509TrustManager tm = new X509TrustManager() { | |
| 19 | + @Override | |
| 20 | + public void checkClientTrusted(X509Certificate[] chain, | |
| 21 | + String authType) throws CertificateException { | |
| 22 | + } | |
| 23 | + @Override | |
| 24 | + public void checkServerTrusted(X509Certificate[] chain, | |
| 25 | + String authType) throws CertificateException { | |
| 26 | + } | |
| 27 | + @Override | |
| 28 | + public X509Certificate[] getAcceptedIssuers() { | |
| 29 | + return null; | |
| 30 | + } | |
| 31 | + }; | |
| 32 | + ctx.init(null, new TrustManager[]{tm}, null); | |
| 33 | + SSLSocketFactory ssf = new SSLSocketFactory(ctx,SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); | |
| 34 | + ClientConnectionManager ccm = this.getConnectionManager(); | |
| 35 | + SchemeRegistry sr = ccm.getSchemeRegistry(); | |
| 36 | + sr.register(new Scheme("https", 443, ssf)); | |
| 37 | + } | |
| 38 | +} |