0

I'm trying to get some JSON information from Ragic, an online database service, and need to query the URL with a API key in the header. I've tried several different encoding methods, but every single time I'm getting back an empty URL, meaning that the API key was missing from the header or the header was simply wrong. I'm not so sure as to what the problem is, the code compiles, but simply returns an empty HTML with simply "{}", meaning there are zero JSONs that were received.

package main;

import java.io.*;
import java.net.*;
import org.apache.commons.codec.binary.Base64;

public class Example {

public static void main(String[] args) throws UnsupportedEncodingException, Exception {
    String apiKey = "apikey";
    try {
        URL url = new URL("https://api.ragic.com/parcare/new-tab/1");
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();

        String basicAuth = "Basic " + new String(new Base64().encode(apiKey.getBytes()));
        basicAuth = basicAuth.replaceAll("\n", "");

        conn.addRequestProperty("Authorization", basicAuth);
        conn.setRequestMethod("POST");
        conn.connect();

        try (BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"))) {
            for (String line; (line = reader.readLine()) != null;) {
                System.out.println(line);
            }
        }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (RuntimeException allDownloadExceptions) {
            allDownloadExceptions.printStackTrace();
        }
    }
}
fedorp1
  • 23
  • 3
  • Pls try `String credentials = "user:pass"; String auth = "Basic " + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);` – BNK Jan 08 '16 at 06:36
  • I'm getting an error that Base64.NO_WRAP, specifically, NO_WRAP, cannot be resolved or isn't a field – fedorp1 Jan 08 '16 at 06:38
  • Oh sorry, I use Android class, for Java only, please read http://stackoverflow.com/questions/13109588/base64-encoding-in-java to see if it can help – BNK Jan 08 '16 at 06:48
  • OK, thank you for your help! – fedorp1 Jan 08 '16 at 07:13

0 Answers0