Hey zhou, I was trying to write a Java application to use with the Bitcoinica API, but I seem to be having trouble with the authentication? I can put in
https://username:[email protected]/api/orders.json?n=5 in my browser and it works fine, but I get an Unauthorized exception thrown when I try it in my application. My application grabs all data that does not require authentication just fine, but anything that requires it throws exceptions. Any idea what is the problem?
This is the code I'm using to request the data.
public String getSomething(String urlStr) throws IOException {
URL url = new URL(urlStr);
HttpURLConnection conn =
(HttpURLConnection) url.openConnection();
if (conn.getResponseCode() != 200) {
throw new IOException(conn.getResponseMessage());
}
// Buffer the result into a string
BufferedReader rd = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = rd.readLine()) != null) {
sb.append(line);
}
rd.close();
conn.disconnect();
return sb.toString();
}