Guest User

Untitled

a guest
May 3rd, 2013
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.99 KB | None | 0 0
  1. public static String postEvent(String path, String agentName, String key, String secret, String postData) {
  2.         //String data = nonce() + postData;
  3.         String nonce = String.valueOf(System.currentTimeMillis()) + "000";
  4.         HashMap<String, String> args = new HashMap<String, String>();
  5.         args.put("nonce", nonce);
  6.         String post_data = buildQueryString(args);
  7.         System.out.println(post_data);
  8.  
  9.         String mtGoxStandartPath = "data.mtgox.com/api/";
  10.         String hash_data = path.substring(path.indexOf(mtGoxStandartPath) + mtGoxStandartPath.length() + 2) + "\0" + post_data;
  11.  
  12.         String signature = signRequest(secret, hash_data);
  13.  
  14.        
  15.         HttpClient client = getNewHttpClient();
  16.         String result = "";
  17.         HttpPost post = new HttpPost(path);
  18.         try {
  19.             //secret = new String(util.Base64.decodeFast(secret), "utf-8");
  20.            
  21.             post.addHeader("User-Agent", agentName);
  22.             post.addHeader("Rest-Key", key);
  23.             post.addHeader("Rest-Sign",  signature); //signature.replaceAll("\n", "")); //
  24.             //post.addHeader("nonce", nonce);
  25.             //post.addHeader("Accept-encoding", "UTF-8");
  26.  
  27.             System.out.println(post.toString());
  28.             HttpResponse response = client.execute(post);
  29.  
  30.             BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
  31.             String line = "";
  32.             while ((line = rd.readLine()) != null) {
  33.                 result += line;
  34.             }
  35.         } catch (IOException e) {
  36.             e.printStackTrace();
  37.         }
  38.         return result;
  39.     }
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46. //Build the query string given a set of query parameters
  47.     private static String buildQueryString(HashMap<String, String> args) {
  48.         String result = new String();
  49.         for (String hashkey : args.keySet()) {
  50.             if (result.length() > 0) {
  51.                 result += '&';
  52.             }
  53.             try {
  54.                 result += URLEncoder.encode(hashkey, ENCODING) + "="
  55.                         + URLEncoder.encode(args.get(hashkey), ENCODING);
  56.             } catch (Exception ex) {
  57.                 //Logger.getLogger(MtGox.class.getName()).log(Level.SEVERE, null, ex);
  58.             }
  59.         }
  60.         return result;
  61.     }
  62.  
  63.     private static String signRequest(String secret, String hash_data) {
  64.         String signature = "";
  65.         try {
  66.             Mac mac = Mac.getInstance(SIGN_HASH_FUNCTION);
  67.             SecretKeySpec secret_spec = new SecretKeySpec(org.apache.commons.codec.binary.Base64.decodeBase64(secret), SIGN_HASH_FUNCTION);
  68.             mac.init(secret_spec);
  69.             signature = org.apache.commons.codec.binary.Base64.encodeBase64String(mac.doFinal(hash_data.getBytes()));
  70.         } catch (NoSuchAlgorithmException | InvalidKeyException e) {
  71.             //Logger.getLogger(MtGox.class.getName()).log(Level.SEVERE, null, e);
  72.             e.printStackTrace();
  73.         }
  74.         return signature;
  75.     }
Advertisement
Add Comment
Please, Sign In to add comment