├── README.md ├── KeepAlive.java ├── Globals.java └── Exchange.java /README.md: -------------------------------------------------------------------------------- 1 | 2 | # 华泰 Web 接口的 Java 版本 3 | 4 | 由匿名群友提供,有兴趣的可以加群 `429011814` 一起讨论 5 | -------------------------------------------------------------------------------- /KeepAlive.java: -------------------------------------------------------------------------------- 1 | package exchanger; 2 | 3 | import java.io.IOException; 4 | import java.util.Base64; 5 | import java.util.TimerTask; 6 | import java.util.regex.Matcher; 7 | import java.util.regex.Pattern; 8 | 9 | import org.apache.http.client.ClientProtocolException; 10 | import org.apache.http.client.methods.HttpGet; 11 | 12 | public class KeepAlive extends TimerTask{ 13 | 14 | @Override 15 | public void run() { 16 | String BalanceParams = Globals.balance().toString(); 17 | Pattern dot = Pattern.compile(", "); 18 | Matcher MatchDot = dot.matcher(BalanceParams); // 把,_改成&并且去掉[] 19 | String temp = MatchDot.replaceAll("&"); 20 | temp = temp.substring(1, temp.length() - 1); 21 | String Params = Exchange.CreatBasicParams() + temp; 22 | System.out.println(Params); // 调试用,输出参数设置 23 | String BalanceURL = Globals.prefix + Base64.getEncoder().encodeToString(Params.getBytes()); 24 | // System.out.println(BalanceURL); // 调试用,输出申请的网页地址 25 | HttpGet getBalance = new HttpGet(BalanceURL); 26 | getBalance.setHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko"); 27 | try { 28 | Exchange.Exchanger.execute(getBalance); 29 | // System.out.println("alive"); //调试用 30 | } catch (ClientProtocolException e) { 31 | e.printStackTrace(); 32 | } catch (IOException e) { 33 | e.printStackTrace(); 34 | } 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /Globals.java: -------------------------------------------------------------------------------- 1 | package exchanger; //都是交易用的网址和参数 2 | 3 | import java.io.BufferedReader; 4 | import java.io.File; 5 | import java.io.FileInputStream; 6 | import java.io.FileNotFoundException; 7 | import java.io.IOException; 8 | import java.io.InputStreamReader; 9 | 10 | import java.net.InetAddress; 11 | import java.net.NetworkInterface; 12 | import java.util.ArrayList; 13 | import java.util.Formatter; 14 | import java.util.List; 15 | import java.util.Locale; 16 | 17 | import org.apache.http.NameValuePair; 18 | import org.apache.http.message.BasicNameValuePair; 19 | 20 | import net.sf.json.JSONObject; 21 | 22 | public class Globals { // 全局变量信息 23 | public static String login_page = "https://service.htsc.com.cn/service/login.jsp"; 24 | public static String login_api = "https://service.htsc.com.cn/service/loginAction.do?method=login"; 25 | public static String trade_info_page = "https://service.htsc.com.cn/service/flashbusiness_new3.jsp?etfCode="; 26 | public static String verify_code_api = "https://service.htsc.com.cn/service/pic/verifyCodeImage.jsp"; 27 | public static String prefix = "https://tradegw.htsc.com.cn/?"; 28 | public static String version = "1"; 29 | public static String userName = null; 30 | public static String trdpwdEns = null; 31 | public static String custid = null; 32 | public static String op_entrust_way = "7"; 33 | public static String password = null; 34 | public static String servicePwd = null; 35 | public static String identity_type = ""; 36 | public static String sh_exchange_type = null; 37 | public static String sh_stock_account = null; 38 | public static String sz_exchange_type = null; 39 | public static String sz_stock_account = null; 40 | public static String fund_account = null; 41 | public static String client_risklevel = null; 42 | public static String op_station = null; 43 | public static String trdpwd = null; 44 | public static String uid = null; 45 | public static String branch_no = null; 46 | public static String op_branch_no = null; 47 | 48 | public static List login() { 49 | String ip = getIp(); // 得到ip地址 50 | String mac = getMac(); // 得到mac地址 51 | String verifycode = Exchange.getVerifyCode(); // 得到验证码 52 | List params = new ArrayList(); 53 | params.add(new BasicNameValuePair("loginEvent", "1")); // easytrader发送的头 54 | params.add(new BasicNameValuePair("topath", "null")); 55 | params.add(new BasicNameValuePair("accountType", "1")); 56 | params.add(new BasicNameValuePair("userType", "jy")); 57 | params.add(new BasicNameValuePair("userName", userName)); 58 | params.add(new BasicNameValuePair("trdpwd", trdpwd)); 59 | params.add(new BasicNameValuePair("trdpwdEns", trdpwdEns)); 60 | params.add(new BasicNameValuePair("servicePwd", servicePwd)); 61 | params.add(new BasicNameValuePair("macaddr", mac)); 62 | params.add(new BasicNameValuePair("lipInfo", ip)); 63 | params.add(new BasicNameValuePair("vcode", verifycode)); 64 | return params; 65 | } 66 | 67 | public static List position() { 68 | List params = new ArrayList(); 69 | params.add(new BasicNameValuePair("cssweb_type", "GET_STOCK_POSITION")); 70 | params.add(new BasicNameValuePair("function_id", "403")); 71 | params.add(new BasicNameValuePair("exchange_type", "")); 72 | params.add(new BasicNameValuePair("stock_account", "")); 73 | params.add(new BasicNameValuePair("stock_code", "")); 74 | params.add(new BasicNameValuePair("query_direction", "")); 75 | params.add(new BasicNameValuePair("query_mode", "0")); 76 | params.add(new BasicNameValuePair("request_num", "100")); 77 | params.add(new BasicNameValuePair("position_str", "")); 78 | return params; 79 | } 80 | 81 | public static List balance() { 82 | List params = new ArrayList(); 83 | params.add(new BasicNameValuePair("cssweb_type", "GET_FUNDS")); 84 | params.add(new BasicNameValuePair("function_id", "405")); 85 | params.add(new BasicNameValuePair("identity_type", "")); 86 | params.add(new BasicNameValuePair("money_type", "")); 87 | return params; 88 | } 89 | 90 | public static List entrust() { 91 | List params = new ArrayList(); 92 | params.add(new BasicNameValuePair("cssweb_type", "GET_CANCEL_LIST")); 93 | params.add(new BasicNameValuePair("function_id", "401")); 94 | params.add(new BasicNameValuePair("exchange_type", "")); 95 | params.add(new BasicNameValuePair("stock_account", "")); 96 | params.add(new BasicNameValuePair("stock_code", "")); 97 | params.add(new BasicNameValuePair("query_direction", "")); 98 | params.add(new BasicNameValuePair("sort_direction", "0")); 99 | params.add(new BasicNameValuePair("request_num", "100")); 100 | params.add(new BasicNameValuePair("position_str", "")); 101 | return params; 102 | } 103 | 104 | public static List buy() { 105 | List params = new ArrayList(); 106 | params.add(new BasicNameValuePair("uid", uid)); 107 | params.add(new BasicNameValuePair("cssweb_type", "STOCK_BUY")); 108 | params.add(new BasicNameValuePair("version", version)); 109 | params.add(new BasicNameValuePair("custid", custid)); 110 | params.add(new BasicNameValuePair("op_branch_no", op_branch_no)); 111 | params.add(new BasicNameValuePair("branch_no", branch_no)); 112 | params.add(new BasicNameValuePair("op_entrust_way", "7")); 113 | params.add(new BasicNameValuePair("op_station", op_station)); 114 | params.add(new BasicNameValuePair("function_id", "301")); 115 | params.add(new BasicNameValuePair("fund_account", userName)); 116 | params.add(new BasicNameValuePair("password", password)); 117 | params.add(new BasicNameValuePair("identity_type", "")); 118 | return params; 119 | } 120 | 121 | public static List sell() { 122 | List params = new ArrayList(); 123 | params.add(new BasicNameValuePair("uid", uid)); 124 | params.add(new BasicNameValuePair("cssweb_type", "STOCK_SELL")); 125 | params.add(new BasicNameValuePair("version", version)); 126 | params.add(new BasicNameValuePair("custid", custid)); 127 | params.add(new BasicNameValuePair("op_branch_no", op_branch_no)); 128 | params.add(new BasicNameValuePair("branch_no", branch_no)); 129 | params.add(new BasicNameValuePair("op_entrust_way", "7")); 130 | params.add(new BasicNameValuePair("op_station", op_station)); 131 | params.add(new BasicNameValuePair("function_id", "302")); 132 | params.add(new BasicNameValuePair("fund_account", userName)); 133 | params.add(new BasicNameValuePair("password", password)); 134 | params.add(new BasicNameValuePair("identity_type", "")); 135 | return params; 136 | } 137 | 138 | public static List cancel_entrust() { 139 | List params = new ArrayList(); 140 | params.add(new BasicNameValuePair("uid", uid)); 141 | params.add(new BasicNameValuePair("cssweb_type", "STOCK_CANCEL")); 142 | params.add(new BasicNameValuePair("version", version)); 143 | params.add(new BasicNameValuePair("custid", custid)); 144 | params.add(new BasicNameValuePair("op_branch_no", op_branch_no)); 145 | params.add(new BasicNameValuePair("branch_no", branch_no)); 146 | params.add(new BasicNameValuePair("op_entrust_way", "7")); 147 | params.add(new BasicNameValuePair("op_station", op_station)); 148 | params.add(new BasicNameValuePair("function_id", "304")); 149 | params.add(new BasicNameValuePair("fund_account", userName)); 150 | params.add(new BasicNameValuePair("password", password)); 151 | params.add(new BasicNameValuePair("identity_type", "")); 152 | return params; 153 | } 154 | 155 | public static String getIp() { // 获取本机ip 156 | String sIP = null; 157 | try { 158 | InetAddress address = InetAddress.getLocalHost(); 159 | sIP = address.getHostAddress(); 160 | } catch (Exception e) { 161 | e.printStackTrace(); 162 | } 163 | return sIP; 164 | } 165 | 166 | public static String getMac() { // 获取本机mac 167 | String sMAC = null; 168 | try { 169 | InetAddress address = InetAddress.getLocalHost(); 170 | NetworkInterface ni = NetworkInterface.getByInetAddress(address); 171 | byte[] mac = ni.getHardwareAddress(); 172 | Formatter formatter = new Formatter(); 173 | for (int i = 0; i < mac.length; i++) { 174 | sMAC = formatter.format(Locale.getDefault(), "%02X%s", mac[i], (i < mac.length - 1) ? "-" : "") 175 | .toString(); 176 | 177 | } 178 | formatter.close(); 179 | } catch (Exception e) { 180 | e.printStackTrace(); 181 | } 182 | return sMAC; 183 | } 184 | 185 | public static void getUserInfo() { // 获取用户登录账号密码信息 186 | String UserInfo = "UserInfo.txt"; // 识别验证码的包的命令 187 | File file = new File(UserInfo); 188 | if (file.isFile() && file.exists()) { // 判断文件是否存在 189 | InputStreamReader read = null; 190 | try { 191 | read = new InputStreamReader(new FileInputStream(file)); 192 | } catch (FileNotFoundException e) { 193 | e.printStackTrace(); 194 | } 195 | BufferedReader bufferedReader = new BufferedReader(read); 196 | String UserInfoJson = null; 197 | try { 198 | UserInfoJson = bufferedReader.readLine(); 199 | } catch (IOException e) { 200 | e.printStackTrace(); 201 | } 202 | // System.out.println(UserInfoJson); //调试用 203 | JSONObject jo = JSONObject.fromObject(UserInfoJson); 204 | // System.out.println(jo.toString()); //调试用 205 | userName = jo.getString("userName"); 206 | servicePwd = jo.getString("servicePwd"); 207 | trdpwd = jo.getString("trdpwd"); 208 | // System.out.println(" userName:"+ userName + " servicePwd:" + servicePwd + " trdpwd:" + trdpwd); //调试用 209 | custid = userName; 210 | fund_account = userName; 211 | trdpwdEns = trdpwd; 212 | password = trdpwd; 213 | try { 214 | read.close(); 215 | } catch (IOException e) { 216 | e.printStackTrace(); 217 | } 218 | } 219 | else { 220 | System.out.println("找不到指定的文件"); 221 | } 222 | 223 | } 224 | } 225 | -------------------------------------------------------------------------------- /Exchange.java: -------------------------------------------------------------------------------- 1 | package exchanger; 2 | 3 | import java.io.ByteArrayOutputStream; 4 | import java.io.FileOutputStream; 5 | import java.io.IOException; 6 | import java.io.InputStream; 7 | import java.io.PrintStream; 8 | import java.io.UnsupportedEncodingException; 9 | import java.util.Base64; 10 | import java.util.List; 11 | import java.util.Random; 12 | import java.util.Timer; 13 | import java.util.regex.Matcher; 14 | import java.util.regex.Pattern; 15 | import net.sf.json.JSONArray; 16 | import net.sf.json.JSONObject; 17 | import javax.swing.JOptionPane; 18 | 19 | import org.apache.http.HttpEntity; 20 | import org.apache.http.HttpResponse; 21 | import org.apache.http.NameValuePair; 22 | import org.apache.http.client.ClientProtocolException; 23 | import org.apache.http.client.entity.UrlEncodedFormEntity; 24 | import org.apache.http.client.methods.HttpGet; 25 | import org.apache.http.client.methods.HttpPost; 26 | import org.apache.http.impl.client.CloseableHttpClient; 27 | import org.apache.http.impl.client.HttpClientBuilder; 28 | import org.apache.http.util.EntityUtils; 29 | import cn.skypark.code.MyCheckCodeTool; 30 | 31 | public class Exchange { // 所有交易用到的方法 32 | static CloseableHttpClient Exchanger = HttpClientBuilder.create().build(); 33 | 34 | public static void Login() { // 自动登录账号信息,包含登录需要的,向登录页请求,向验证码页请求, 35 | Globals.getUserInfo(); // 获取登录需要的账号信息 36 | HttpGet getLoginPage = new HttpGet(Globals.login_page); 37 | try { 38 | Exchanger.execute(getLoginPage); // 发起对登录页的GET请求 39 | // System.out.println(" "); // 调试用断点位置 40 | // HttpResponse responseLoginPage = Exchanger.execute(getLoginPage); 41 | // // 调试用,发起对登录页的GET请求 42 | // System.out.println("result = " + 43 | // EntityUtils.toString(responseLoginPage.getEntity(), "utf-8")); // 44 | // 调试用,获取登录页的内容 45 | // System.out.println("resCode = " + 46 | // responseLoginPage.getStatusLine().getStatusCode()); // 调试用,获取响应码 47 | List params = Globals.login(); 48 | HttpPost postLoginInfo = new HttpPost(Globals.login_api); 49 | try { 50 | postLoginInfo.setEntity(new UrlEncodedFormEntity(params, "utf-8")); // 设置发送的登录信息 51 | // HttpEntity postEntity = postLoginInfo.getEntity(); // 调试用 52 | // if (postEntity != null) { // 调试用 53 | // System.out.println("Post content: " + 54 | // EntityUtils.toString(postEntity, "utf-8")); // 55 | // 调试用,显示向登录页传递的账号信息 56 | // } // 调试用 57 | HttpResponse responseLogin = Exchanger.execute(postLoginInfo); // 向登录页传递账号信息 58 | HttpEntity responseEntity = responseLogin.getEntity(); 59 | if (responseEntity != null) { 60 | String LoginInfo = EntityUtils.toString(responseEntity, "utf-8"); 61 | // System.out.println("Response content: " + LoginInfo); // 62 | // 调试用,获取已登录的页 63 | int i = LoginInfo.indexOf("欢迎您"); 64 | if (i != -1) { 65 | GetTradeInfo(); 66 | } else { 67 | JOptionPane.showMessageDialog(null, "登录失败!", "消息提示", JOptionPane.ERROR_MESSAGE); 68 | } 69 | } 70 | } catch (ClientProtocolException e) { 71 | e.printStackTrace(); 72 | } catch (UnsupportedEncodingException e1) { 73 | e1.printStackTrace(); 74 | } catch (IOException e) { 75 | e.printStackTrace(); 76 | } 77 | } catch (ClientProtocolException e1) { 78 | e1.printStackTrace(); 79 | } catch (IOException e2) { 80 | e2.printStackTrace(); 81 | } 82 | } 83 | 84 | public static String getVerifyCode() { // 通过调用getcode_jdk1.8.jar包获取验证码 85 | Random random = new Random(); 86 | int r = random.nextInt(); 87 | HttpGet getVerifyImage = new HttpGet(Globals.verify_code_api + "?" + r); 88 | String VerifyCode = null; 89 | try { 90 | HttpResponse responseVerifyImage = Exchanger.execute(getVerifyImage); // 发起GET请求 91 | InputStream inputVerifyImage = responseVerifyImage.getEntity().getContent(); 92 | FileOutputStream outputVerifyImage = new FileOutputStream("VerifyCode.jpg"); // 将GET到的验证码保存为图片 93 | byte[] data = new byte[1024]; 94 | int len = 0; 95 | while ((len = inputVerifyImage.read(data)) != -1) { 96 | outputVerifyImage.write(data, 0, len); 97 | } 98 | outputVerifyImage.close(); 99 | VerifyCode = Image2Code(); // 运行验证码识别包 100 | } catch (ClientProtocolException e1) { 101 | e1.printStackTrace(); 102 | } catch (IOException e2) { 103 | e2.printStackTrace(); 104 | } 105 | return VerifyCode; 106 | } 107 | 108 | public static String Image2Code() { // 将验证码保存为图片存于文件目录 109 | String result = null; 110 | String CodeImage = "VerifyCode.jpg"; // 识别验证码的包的命令 111 | String[] getCodeArg = new String[] { CodeImage }; 112 | try { 113 | ByteArrayOutputStream bao = new ByteArrayOutputStream(); 114 | PrintStream ps = new PrintStream(bao); 115 | PrintStream so = System.out; 116 | System.setOut(ps); 117 | MyCheckCodeTool.main(getCodeArg); // 截取命令行返回结果 118 | result = bao.toString(); 119 | System.setOut(so); 120 | result = result.substring(26, 30); // 仅保留验证码部分 121 | // System.out.println(result); 122 | } catch (Exception e) { 123 | e.printStackTrace(); 124 | } 125 | return result; 126 | } 127 | 128 | public static void GetTradeInfo() { // 获取交易用的uid 和 password信息 129 | HttpGet getTradeInfo = new HttpGet(Globals.trade_info_page); 130 | String TradeInfo = null; 131 | try { 132 | HttpResponse responseTradeInfo = Exchanger.execute(getTradeInfo); // 发起对登录信息页的GET请求 133 | HttpEntity responseEntity = responseTradeInfo.getEntity(); 134 | if (responseEntity != null) { 135 | TradeInfo = EntityUtils.toString(responseEntity, "utf-8"); 136 | // System.out.println("Response content: " + TradeInfo); // 137 | // 调试用,获取登录信息页 138 | } 139 | String JsonBase64 = " var data = \"([/=\\w\\+]+)\""; // 得到base64的正则表达式 140 | Pattern Base64Pattern = Pattern.compile(JsonBase64); 141 | Matcher MatchBase64 = Base64Pattern.matcher(TradeInfo); // 匹配交易信息中的正则表达式 142 | MatchBase64.find(); 143 | String temp = MatchBase64.toString(); 144 | // System.out.println(temp); // 调试用,输出base64的交易信息 145 | int TradeInfoJsonLength = temp.length(); // 输出匹配后的base64的交易信息长度 146 | // System.out.println(TradeInfoJsonLength); // 调试用,输出交易信息长度 147 | String TradeInfoJsonBase64 = temp.substring(95, TradeInfoJsonLength - 2); // 使用偏移量移除匹配的var 148 | // data 149 | // = 150 | // 字段 151 | // System.out.println(TradeInfoJsonBase64); // 调试用,输出base64编码的的交易信息 152 | byte[] TradeInfoJsonByte = Base64.getDecoder().decode(TradeInfoJsonBase64); // 将base64格式解码 153 | String TradeInfoJson = null; 154 | try { 155 | TradeInfoJson = new String(TradeInfoJsonByte, "utf-8"); // 对解码后使用utf-8编码得到json格式交易信息 156 | } catch (UnsupportedEncodingException e2) { 157 | e2.printStackTrace(); 158 | } 159 | // System.out.println(TradeInfoJson); // 调试用,输出json格式的交易信息 160 | JSONObject jo = JSONObject.fromObject(TradeInfoJson); 161 | JSONArray ja = jo.getJSONArray("item"); 162 | JSONObject shanghai = ja.getJSONObject(0); // 读取上海交易账号信息 163 | Globals.sh_exchange_type = shanghai.getString("exchange_type"); 164 | Globals.sh_stock_account = shanghai.getString("stock_account"); 165 | JSONObject shenzhen = ja.getJSONObject(1); // 读取深圳交易账号信息 166 | Globals.sz_exchange_type = shenzhen.getString("exchange_type"); 167 | Globals.sz_stock_account = shenzhen.getString("stock_account"); 168 | Globals.client_risklevel = jo.getString("branch_no"); 169 | Globals.op_station = jo.getString("op_station"); 170 | Globals.trdpwd = jo.getString("trdpwd"); 171 | Globals.uid = jo.getString("uid"); 172 | Globals.branch_no = jo.getString("branch_no"); 173 | Globals.op_branch_no = jo.getString("branch_no"); 174 | } catch (IOException e) { 175 | e.printStackTrace(); 176 | } 177 | } 178 | 179 | public static String GetPosition() { // 获取账户持仓状况 180 | String Position = null; 181 | String PositionParams = Globals.position().toString(); 182 | Pattern dot = Pattern.compile(", "); 183 | Matcher MatchDot = dot.matcher(PositionParams); // 把,_改成&并且去掉[] 184 | String temp = MatchDot.replaceAll("&"); 185 | temp = temp.substring(1, temp.length() - 1); 186 | String Params = CreatBasicParams() + temp; 187 | // System.out.println(Params); // 调试用,输出参数设置 188 | String PositionURL = Globals.prefix + Base64.getEncoder().encodeToString(Params.getBytes()); 189 | // System.out.println(PositionURL); // 调试用,输出申请的网页地址 190 | HttpGet getPosition = new HttpGet(PositionURL); 191 | getPosition.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko"); 192 | try { 193 | Exchanger.execute(getPosition); 194 | // System.out.println(" "); // 调试用断点位置 195 | HttpResponse responseGetPosition = Exchanger.execute(getPosition); // 调试用,发起对登录页的GET请求 196 | String PositionJsonBase64 = EntityUtils.toString(responseGetPosition.getEntity(), "utf-8"); // 获取登录页的内容 197 | byte[] PositionJsonByte = Base64.getDecoder().decode(PositionJsonBase64); // 将base64格式解码 198 | String PositionJson = null; 199 | try { 200 | PositionJson = new String(PositionJsonByte, "utf-8"); // 对解码后使用utf-8编码得到json格式交易信息 201 | Position = PositionJson; 202 | // 处理json 203 | System.out.println(PositionJson); // 调试用 204 | } catch (UnsupportedEncodingException e2) { 205 | e2.printStackTrace(); 206 | } 207 | } catch (ClientProtocolException e) { 208 | e.printStackTrace(); 209 | } catch (IOException e) { 210 | e.printStackTrace(); 211 | } 212 | return Position; 213 | } 214 | 215 | public static String GetBalance() { // 获取账户资金状况 216 | String Balance = null; 217 | String BalanceParams = Globals.balance().toString(); 218 | Pattern dot = Pattern.compile(", "); 219 | Matcher MatchDot = dot.matcher(BalanceParams); // 把,_改成&并且去掉[] 220 | String temp = MatchDot.replaceAll("&"); 221 | temp = temp.substring(1, temp.length() - 1); 222 | String Params = CreatBasicParams() + temp; 223 | // System.out.println(Params); // 调试用,输出参数设置 224 | String BalanceURL = Globals.prefix + Base64.getEncoder().encodeToString(Params.getBytes()); 225 | // System.out.println(BalanceURL); // 调试用,输出申请的网页地址 226 | HttpGet getBalance = new HttpGet(BalanceURL); 227 | getBalance.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko"); 228 | try { 229 | Exchanger.execute(getBalance); 230 | // System.out.println(" "); // 调试用断点位置 231 | HttpResponse responseGetBalance = Exchanger.execute(getBalance); // 调试用,发起对登录页的GET请求 232 | String BalanceJsonBase64 = EntityUtils.toString(responseGetBalance.getEntity(), "utf-8"); // 获取登录页的内容 233 | byte[] BalanceJsonByte = Base64.getDecoder().decode(BalanceJsonBase64); // 将base64格式解码 234 | String BalanceJson = null; 235 | try { 236 | BalanceJson = new String(BalanceJsonByte, "utf-8"); // 对解码后使用utf-8编码得到json格式交易信息 237 | Balance = BalanceJson; 238 | // 处理json 239 | // System.out.println(BalanceJson); //调试用 240 | } catch (UnsupportedEncodingException e2) { 241 | e2.printStackTrace(); 242 | } 243 | } catch (ClientProtocolException e) { 244 | e.printStackTrace(); 245 | } catch (IOException e) { 246 | e.printStackTrace(); 247 | } 248 | return Balance; 249 | } 250 | 251 | public static String GetEntrust() { // 获取当日委托单 252 | String Entrust = null; 253 | String EntrustParams = Globals.entrust().toString(); 254 | Pattern dot = Pattern.compile(", "); 255 | Matcher MatchDot = dot.matcher(EntrustParams); // 把,_改成&并且去掉[] 256 | String temp = MatchDot.replaceAll("&"); 257 | temp = temp.substring(1, temp.length() - 1); 258 | String Params = CreatBasicParams() + temp; 259 | // System.out.println(Params); // 调试用,输出参数设置 260 | String EntrustURL = Globals.prefix + Base64.getEncoder().encodeToString(Params.getBytes()); 261 | // System.out.println(EntrustURL); // 调试用,输出申请的网页地址 262 | HttpGet getEntrust = new HttpGet(EntrustURL); 263 | getEntrust.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko"); 264 | try { 265 | Exchanger.execute(getEntrust); 266 | // System.out.println(" "); // 调试用断点位置 267 | HttpResponse responseGetEntrust = Exchanger.execute(getEntrust); // 调试用,发起对登录页的GET请求 268 | String EntrustJsonBase64 = EntityUtils.toString(responseGetEntrust.getEntity(), "utf-8"); // 获取登录页的内容 269 | byte[] EntrustJsonByte = Base64.getDecoder().decode(EntrustJsonBase64); // 将base64格式解码 270 | String EntrustJson = null; 271 | try { 272 | EntrustJson = new String(EntrustJsonByte, "utf-8"); // 对解码后使用utf-8编码得到json格式交易信息 273 | Entrust = EntrustJson; 274 | // 处理json 275 | // System.out.println(Entrust); //调试用 276 | } catch (UnsupportedEncodingException e2) { 277 | e2.printStackTrace(); 278 | } 279 | } catch (ClientProtocolException e) { 280 | e.printStackTrace(); 281 | } catch (IOException e) { 282 | e.printStackTrace(); 283 | } 284 | return Entrust; 285 | } 286 | 287 | public static String CancelEntrust(int EntrustNo) { // 取消委托单 288 | String CancelEntrust = null; 289 | String CancelEntrustParams = Globals.cancel_entrust().toString(); 290 | Pattern dot = Pattern.compile(", "); 291 | Matcher MatchDot = dot.matcher(CancelEntrustParams); // 把,_改成&并且去掉[] 292 | String temp = MatchDot.replaceAll("&"); 293 | temp = temp.substring(1, temp.length() - 1); 294 | String password = Globals.trdpwd; 295 | Random random = new Random(); 296 | int r = random.nextInt(); 297 | int entrust_no = EntrustNo; // 要取消的委托单号 298 | temp = temp + "&" + "password=" + password + "&" + "entrust_no=" + entrust_no + "&" + "ram=" + r; 299 | String Params = temp; 300 | // System.out.println(Params); // 调试用,输出参数设置 301 | String CancelEntrustURL = Globals.prefix + Base64.getEncoder().encodeToString(Params.getBytes()); 302 | System.out.println(CancelEntrustURL); // 调试用,输出申请的网页地址 303 | HttpGet getCancelEntrust = new HttpGet(CancelEntrustURL); 304 | getCancelEntrust.setHeader("User-Agent", 305 | "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko"); 306 | try { 307 | Exchanger.execute(getCancelEntrust); 308 | // System.out.println(" "); // 调试用断点位置 309 | HttpResponse responseGetCancelEntrust = Exchanger.execute(getCancelEntrust); // 调试用,发起对登录页的GET请求 310 | String CancelEntrustJsonBase64 = EntityUtils.toString(responseGetCancelEntrust.getEntity(), "utf-8"); // 获取登录页的内容 311 | byte[] CancelEntrustJsonByte = Base64.getDecoder().decode(CancelEntrustJsonBase64); // 将base64格式解码 312 | String CancelEntrustJson = null; 313 | try { 314 | CancelEntrustJson = new String(CancelEntrustJsonByte, "utf-8"); // 对解码后使用utf-8编码得到json格式交易信息 315 | CancelEntrust = CancelEntrustJson; 316 | // 处理json 317 | System.out.println(CancelEntrust); // 调试用 318 | } catch (UnsupportedEncodingException e2) { 319 | e2.printStackTrace(); 320 | } 321 | } catch (ClientProtocolException e) { 322 | e.printStackTrace(); 323 | } catch (IOException e) { 324 | e.printStackTrace(); 325 | } 326 | return CancelEntrust; 327 | } 328 | 329 | public static String Buy(int StockCode, double Price, int Amount) { // 买 330 | String Buy = null; 331 | String BuyParams = Globals.buy().toString(); 332 | Pattern dot = Pattern.compile(", "); 333 | Matcher MatchDot = dot.matcher(BuyParams); // 把,_改成&并且去掉[] 334 | String temp = MatchDot.replaceAll("&"); 335 | temp = temp.substring(1, temp.length() - 1); 336 | int stock_code = StockCode; // 股票代码 337 | int exchange_type = selectSHSZ(stock_code); // 选择上海深圳 338 | String stock_account = null; 339 | if (exchange_type == 1) { 340 | stock_account = Globals.sh_stock_account; 341 | } else { 342 | stock_account = Globals.sz_stock_account; 343 | } 344 | int entrust_amount = Amount; // 买入总股数 345 | double entrust_price = Price; // 买入价格 346 | // int volume = Volume; // 买入总金额 由 volume / price 取 100 的整数, 若指定 amount 347 | // 则此参数无效 348 | int entrust_prop = 0; // 委托类型,暂未实现,默认为限价委托 349 | int entrust_bs = 1; 350 | Random random = new Random(); 351 | int ram = random.nextInt(); 352 | String Params = temp + "&" + "exchange_type=" + exchange_type + "&" + "stock_account=" + stock_account + "&" 353 | + "stock_code=" + stock_code + "&" + "entrust_amount=" + entrust_amount + "&" + "entrust_price=" 354 | + entrust_price + "&" + "entrust_prop=" + entrust_prop + "&" + "entrust_bs=" + entrust_bs + "&" + "ram=" 355 | + ram; 356 | // System.out.println(Params); // 调试用,输出参数设置 357 | String BuyURL = Globals.prefix + Base64.getEncoder().encodeToString(Params.getBytes()); 358 | // System.out.println(BuyURL); // 调试用,输出申请的网页地址 359 | HttpGet getBuy = new HttpGet(BuyURL); 360 | getBuy.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko"); 361 | try { 362 | Exchanger.execute(getBuy); 363 | // System.out.println(" "); // 调试用断点位置 364 | HttpResponse responseGetBuy = Exchanger.execute(getBuy); // 调试用,发起对登录页的GET请求 365 | String BuyJsonBase64 = EntityUtils.toString(responseGetBuy.getEntity(), "utf-8"); // 获取登录页的内容 366 | byte[] BuyJsonByte = Base64.getDecoder().decode(BuyJsonBase64); // 将base64格式解码 367 | String BuyJson = null; 368 | try { 369 | BuyJson = new String(BuyJsonByte, "utf-8"); // 对解码后使用utf-8编码得到json格式交易信息 370 | Buy = BuyJson; 371 | // 处理json 372 | // System.out.println(Buy); // 调试用 373 | } catch (UnsupportedEncodingException e2) { 374 | e2.printStackTrace(); 375 | } 376 | } catch (ClientProtocolException e) { 377 | e.printStackTrace(); 378 | } catch (IOException e) { 379 | e.printStackTrace(); 380 | } 381 | return Buy; 382 | } 383 | 384 | public static String Sell(int StockCode, double Price, int Amount) { // 卖 385 | String Sell = null; 386 | String SellParams = Globals.sell().toString(); 387 | Pattern dot = Pattern.compile(", "); 388 | Matcher MatchDot = dot.matcher(SellParams); // 把,_改成&并且去掉[] 389 | String temp = MatchDot.replaceAll("&"); 390 | temp = temp.substring(1, temp.length() - 1); 391 | int stock_code = StockCode; // 股票代码 392 | int exchange_type = selectSHSZ(stock_code); // 选择上海深圳 393 | String stock_account = null; 394 | if (exchange_type == 1) { 395 | stock_account = Globals.sh_stock_account; 396 | } else { 397 | stock_account = Globals.sz_stock_account; 398 | } 399 | int entrust_amount = Amount; // 买入总股数 400 | double entrust_price = Price; // 买入价格 401 | // int volume = Volume; // 买入总金额 由 volume / price 取 100 的整数, 若指定 amount 402 | // 则此参数无效 403 | int entrust_prop = 0; // 委托类型,暂未实现,默认为限价委托 404 | int entrust_bs = 1; 405 | Random random = new Random(); 406 | int ram = random.nextInt(); 407 | String Params = temp + "&" + "exchange_type=" + exchange_type + "&" + "stock_account=" + stock_account + "&" 408 | + "stock_code=" + stock_code + "&" + "entrust_amount=" + entrust_amount + "&" + "entrust_price=" 409 | + entrust_price + "&" + "entrust_prop=" + entrust_prop + "&" + "entrust_bs=" + entrust_bs + "&" + "ram=" 410 | + ram; 411 | // System.out.println(Params); // 调试用,输出参数设置 412 | String SellURL = Globals.prefix + Base64.getEncoder().encodeToString(Params.getBytes()); 413 | // System.out.println(SellURL); // 调试用,输出申请的网页地址 414 | HttpGet getSell = new HttpGet(SellURL); 415 | getSell.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko"); 416 | try { 417 | Exchanger.execute(getSell); 418 | // System.out.println(" "); // 调试用断点位置 419 | HttpResponse responseGetSell = Exchanger.execute(getSell); // 调试用,发起对登录页的GET请求 420 | String SellJsonBase64 = EntityUtils.toString(responseGetSell.getEntity(), "utf-8"); // 获取登录页的内容 421 | byte[] SellJsonByte = Base64.getDecoder().decode(SellJsonBase64); // 将base64格式解码 422 | String SellJson = null; 423 | try { 424 | SellJson = new String(SellJsonByte, "utf-8"); // 对解码后使用utf-8编码得到json格式交易信息 425 | Sell = SellJson; 426 | // 处理json 427 | // System.out.println(Sell); // 调试用 428 | } catch (UnsupportedEncodingException e2) { 429 | e2.printStackTrace(); 430 | } 431 | } catch (ClientProtocolException e) { 432 | e.printStackTrace(); 433 | } catch (IOException e) { 434 | e.printStackTrace(); 435 | } 436 | return Sell; 437 | } 438 | 439 | public static String CreatBasicParams() { // 返回交易所需的基本参数 440 | Random random = new Random(); 441 | int r = random.nextInt(); 442 | String op_branch_no = Globals.branch_no; 443 | String BasicParams = "uid=" + Globals.uid + "&" + "version=" + Globals.version + "&" + "custid=" 444 | + Globals.custid + "&" + "op_branch_no=" + op_branch_no + "&" + "branch_no=" + Globals.branch_no + "&" 445 | + "op_entrust_way=" + Globals.op_entrust_way + "&" + "op_station=" + Globals.op_station + "&" 446 | + "fund_account=" + Globals.fund_account + "&" + "password=" + Globals.password + "&" + "identity_type=" 447 | + Globals.identity_type + "&" + "ram=" + r; 448 | // System.out.println(BasicParams); // 调试用,输出基本参数 449 | return BasicParams; 450 | } 451 | 452 | public static int selectSHSZ(int StockCode) { // 识别股票代码首字符判断深沪市 453 | String stockcode = Integer.toString(StockCode); 454 | String temp = stockcode.substring(0, 1); 455 | if (temp.equals("5") || temp.equals("6") || temp.equals("9")) { 456 | return 1; // 1是上海 457 | } else { 458 | return 2; // 2是深圳 459 | } 460 | } 461 | 462 | public static void keepAlive() { // 每30秒尝试获取一次账户资金信息以保持会话 463 | Timer timer = new Timer(); 464 | timer.schedule(new KeepAlive(), 30 * 1000, 30 * 1000); 465 | } 466 | 467 | } --------------------------------------------------------------------------------