Advertisement
Guest User

mtgclient

a guest
Apr 15th, 2013
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. public String query(String path, HashMap<String, String> args) {
  2. String answer = "";
  3. try {
  4. // add nonce and build arg list
  5. args.put("nonce", String.valueOf(System.currentTimeMillis()));
  6. String post_data = this.buildQueryString(args);
  7. String post_data_mac = path + "\0" + post_data;
  8.  
  9. // args signature
  10. Mac mac = Mac.getInstance(SIGN_HASH_FUNCTION);
  11. SecretKeySpec secret_spec = new SecretKeySpec(Base64.decodeBase64(this.keys.getPrivateKey()), SIGN_HASH_FUNCTION);
  12. mac.init(secret_spec);
  13. String signature = Base64.encodeBase64String(mac.doFinal(post_data_mac.getBytes()));
  14.  
  15. // build URL
  16. URL queryUrl = new URL(API_BASE_URL + path);
  17.  
  18. // create connection
  19. HttpURLConnection connection = (HttpURLConnection)queryUrl.openConnection();
  20. connection.setDoOutput(true);
  21.  
  22. // set signature
  23. connection.setRequestProperty("User-Agent", settings.APP_TITLE);
  24. connection.setRequestProperty("Rest-Key", this.keys.getApiKey());
  25. connection.setRequestProperty("Rest-Sign", signature.replaceAll("\n", ""));
  26.  
  27.  
  28. if (connection.getResponseCode() != 200) {
  29. System.err.println("Failed : HTTP error code : "
  30. + connection.getResponseCode());
  31. }
  32.  
  33. BufferedReader br = new BufferedReader(new InputStreamReader((connection.getInputStream())));
  34.  
  35. String output;
  36. System.out.println("HTTP response \n");
  37. while ((output = br.readLine()) != null) {
  38. System.out.println(output);
  39. answer+=output;
  40. }
  41. connection.disconnect();
  42.  
  43. } catch (Exception ex) {
  44. Logger.getLogger(MtGox.class.getName()).log(Level.SEVERE, null, ex);
  45. }
  46. return answer;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement