1 /**

2 * 处理数据,在各自档案service实现逻辑3 *@param

4 *@return

5 */

6 public abstract MapsyncData (String result);7

8

9

10 /**

11 * 返回请求参数12 *@return

13 */

14 public abstractJsonArray getRequestData();15

16 /**

17 * 调用查询接口18 *@return

19 */

20 publicString getQueryAdress(){21 returnbaseProperties.getBillQueryUrl();22 }23

24

25 public boolean login() throwsException {26

27 boolean bResult = false;28 JsonArray jParas = newJsonArray();29 jParas.add(baseProperties.getDbId());30 jParas.add(baseProperties.getUserName());31 jParas.add(baseProperties.getPwd());32 jParas.add(baseProperties.getLang());33 HttpURLConnection connection =initUrlConn(baseProperties.getLoginUrl(), jParas);34 String key = null;35 for (int i = 1; (key = connection.getHeaderFieldKey(i)) != null; i++) {36 if (key.equalsIgnoreCase("Set-Cookie")) {37 String tempCookieVal =connection.getHeaderField(i);38 if (tempCookieVal.startsWith("kdservice-sessionid")) {39 CookieVal =tempCookieVal;40 break;41 }42 }43 }44

45 BufferedReader reader = new BufferedReader(newInputStreamReader(46 connection.getInputStream()));47 String line;48 while ((line = reader.readLine()) != null) {49 String sResult = new String(line.getBytes(), "utf-8");50 bResult = line.contains("\"LoginResultType\":1");51 }52 reader.close();53 connection.disconnect();54 returnbResult;55 }56

57 privateHttpURLConnection initUrlConn(String url, JsonArray paras)58 throwsException {59 URL postUrl = newURL(baseProperties.getBaseUrl().concat(url));60 HttpURLConnection connection =(HttpURLConnection) postUrl61 .openConnection();62 if (CookieVal != null) {63 connection.setRequestProperty("Cookie", CookieVal);64 }65 if (!connection.getDoOutput()) {66 connection.setDoOutput(true);67 }68 connection.setRequestMethod("POST");69 connection.setUseCaches(false);70 connection.setInstanceFollowRedirects(true);71 connection.setRequestProperty("Content-Type", "application/json");72 DataOutputStream out = newDataOutputStream(73 connection.getOutputStream());74

75 UUID uuid =UUID.randomUUID();76 int hashCode =uuid.toString().hashCode();77

78 JsonObject jObj = newJsonObject();79 jObj.addProperty("format", 1);80 jObj.addProperty("useragent", "ApiClient");81 jObj.addProperty("rid", hashCode);82 jObj.addProperty("parameters", chinaToUnicode(paras.toString()));83 jObj.addProperty("timestamp", newDate().toString());84 jObj.addProperty("v", "1.0");85

86 out.writeBytes(jObj.toString());87 out.flush();88 out.close();89

90 returnconnection;91 }92

93

94 /**

95 * 拉取数据并处理96 *@return

97 */

98 public Map datax() throwsException {99 login();100 HttpURLConnection connectionInvoke =initUrlConn(getQueryAdress(), getRequestData());101 BufferedReader reader = new BufferedReader(newInputStreamReader(102 connectionInvoke.getInputStream()));103 String line;104 String sResult = null;105 while ((line = reader.readLine()) != null) {106 sResult = new String(line.getBytes(), "utf-8");107 System.out.println(sResult);108 }109 if(sResult.contains("\"IsSuccess\":false")){110 ErrorEntity errorEntity = JSONUtil.toBean(JSONUtil.parseFromMap(objectMapper.readValue(sResult,Map.class)),ErrorEntity.class);111 throw new InfException(StrUtil.format("数据同步失败,当前同步类型为:{},返回信息为:{}",getCurrentType().getTitle(), errorEntity.getResult().getResponseStatus().getErrors().get(0).getMessage()));112 }113 reader.close();114 connectionInvoke.disconnect();115 Map syncMap =syncData(sResult);116 returnsyncMap;117 }118

119 public staticString chinaToUnicode(String str) {120 String result = "";121 for (int i = 0; i < str.length(); i++) {122 int chr1 = (char) str.charAt(i);123 if (chr1 >= 19968 && chr1 <= 171941) {124 result += "\\u" +Integer.toHexString(chr1);125 } else{126 result +=str.charAt(i);127 }128 }129 returnresult;130 }

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐