Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public String query(String path, HashMap<String, String> args) {
- String answer = "";
- boolean httpError = false;
- HttpsURLConnection connection=null;
- String nonce = String.valueOf(System.currentTimeMillis());
- try {
- // add nonce and build arg list
- args.put("nonce", nonce);
- String hash_data = path + "\0" + this.buildQueryString(args); //Should be correct
- System.out.println("post-data-mac: "+ hash_data);
- // args signature with apache cryptografic tools
- String signature = signRequest(this.keys.getPrivateKey(), hash_data);
- // build URL
- URL queryUrl = new URL(API_BASE_URL + path);
- // create and setup a HTTP connection
- connection = (HttpsURLConnection)queryUrl.openConnection();
- //Check if the call should be done over POST or GET, depends on the function
- if(path.equals(API_TICKER))
- connection.setRequestMethod("GET");
- else
- connection.setRequestMethod("POST");
- connection.setRequestProperty("User-Agent", settings.APP_TITLE);
- connection.setRequestProperty("Rest-Key", this.keys.getApiKey());
- connection.setRequestProperty("Rest-Sign", signature.replaceAll("\n", ""));
- connection.setReadTimeout(10000);
- connection.setDoOutput(true);
- connection.setDoInput(true);
- //Read the response
- BufferedReader br = null;
- if (connection.getResponseCode() >= 400) {
- httpError = true;//TODO , if HTTP error, do something else with output!
- br = new BufferedReader(new InputStreamReader((connection.getErrorStream())));
- }
- else
- br = new BufferedReader(new InputStreamReader((connection.getInputStream())));
- String output;
- System.out.println("HTTP response : \n"); //TODO Remove
- while ((output = br.readLine()) != null) {
- System.out.println(output);
- answer+=output;
- }
- }
- //Capture Exceptions
- catch (IllegalStateException ex) {
- Logger.getLogger(MtGox.class.getName()).log(Level.SEVERE, null, ex);
- }
- catch (IOException ex) {
- Logger.getLogger(MtGox.class.getName()).log(Level.SEVERE, null, ex);
- }
- finally
- {
- //close the connection, set all objects to null
- connection.disconnect();
- connection = null;
- }
- return answer;
- }
- //Build the query string given a set of query parameters
- protected 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 String signRequest(String secret, String hash_data) {
- String signature = "";
- try{
- Mac mac = Mac.getInstance(SIGN_HASH_FUNCTION);
- SecretKeySpec secret_spec = new SecretKeySpec(Base64.decodeBase64(secret), SIGN_HASH_FUNCTION);
- mac.init(secret_spec);
- signature = Base64.encodeBase64String(mac.doFinal(hash_data.getBytes()));
- }
- catch (NoSuchAlgorithmException | InvalidKeyException e){
- Logger.getLogger(MtGox.class.getName()).log(Level.SEVERE, null, e);
- }
- return signature;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement