Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static String postEvent(String path, String agentName, String key, String secret, String postData) {
- //String data = nonce() + postData;
- String nonce = String.valueOf(System.currentTimeMillis()) + "000";
- HashMap<String, String> args = new HashMap<String, String>();
- args.put("nonce", nonce);
- String post_data = buildQueryString(args);
- System.out.println(post_data);
- String mtGoxStandartPath = "data.mtgox.com/api/";
- String hash_data = path.substring(path.indexOf(mtGoxStandartPath) + mtGoxStandartPath.length() + 2) + "\0" + post_data;
- String signature = signRequest(secret, hash_data);
- HttpClient client = getNewHttpClient();
- String result = "";
- HttpPost post = new HttpPost(path);
- try {
- //secret = new String(util.Base64.decodeFast(secret), "utf-8");
- post.addHeader("User-Agent", agentName);
- post.addHeader("Rest-Key", key);
- post.addHeader("Rest-Sign", signature); //signature.replaceAll("\n", "")); //
- //post.addHeader("nonce", nonce);
- //post.addHeader("Accept-encoding", "UTF-8");
- System.out.println(post.toString());
- HttpResponse response = client.execute(post);
- BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
- String line = "";
- while ((line = rd.readLine()) != null) {
- result += line;
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- return result;
- }
- //Build the query string given a set of query parameters
- private static String buildQueryString(HashMap<String, String> args) {
- String result = new String();
- for (String hashkey : args.keySet()) {
- if (result.length() > 0) {
- result += '&';
- }
- try {
- result += URLEncoder.encode(hashkey, ENCODING) + "="
- + URLEncoder.encode(args.get(hashkey), ENCODING);
- } catch (Exception ex) {
- //Logger.getLogger(MtGox.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- return result;
- }
- private static String signRequest(String secret, String hash_data) {
- String signature = "";
- try {
- Mac mac = Mac.getInstance(SIGN_HASH_FUNCTION);
- SecretKeySpec secret_spec = new SecretKeySpec(org.apache.commons.codec.binary.Base64.decodeBase64(secret), SIGN_HASH_FUNCTION);
- mac.init(secret_spec);
- signature = org.apache.commons.codec.binary.Base64.encodeBase64String(mac.doFinal(hash_data.getBytes()));
- } catch (NoSuchAlgorithmException | InvalidKeyException e) {
- //Logger.getLogger(MtGox.class.getName()).log(Level.SEVERE, null, e);
- e.printStackTrace();
- }
- return signature;
- }
Advertisement
Add Comment
Please, Sign In to add comment