0

I started bitnami's iso on vmware machine. Standard administration login and pass for bitnami is : login: user, pass: bitnami.

In Authentication tab is selected Authentication required, Enable REST web service and enable JSONP support.

Any body knows, why I get below statement? For any help i will be very grateful.

I got a statement :

Exception in thread "main" java.lang.RuntimeException: Failed : HTTP
error code : 401 at NetClientGet.main(NetClientGet.java:36)
Java Result: 1

My code:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class NetClientGet {

    public static void main(String[] args) {

      try {
        URL url = new URL("http://192.168.0.78/issues.json?key=pjxjDvD9ez0Qm97iApka");

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setRequestProperty("Accept", "application/json");
        conn.setDoOutput(true);

        if (conn.getResponseCode() != 200) {
          throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
        }

        BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));

        String output;
        System.out.println("Output from Server .... \n");
        while ((output = br.readLine()) != null) {
          System.out.println(output);
        }
        conn.disconnect();

      } catch (MalformedURLException e) {
        e.printStackTrace();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
}
basgys
  • 4,320
  • 28
  • 39

1 Answers1

0

An HTTP status code 401 means you have an authentication problem. That has nothing to do with your code.

However throwing a RuntimeException for a bad status code is rough.

if (conn.getResponseCode() != 200) {
  throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
}

Maybe you can handle different scenarios gracefully. Example :

if (conn.getResponseCode() >= 300) {
  // Something unexpected happened
  System.out.println("Unexpected response from server");
  return // Stop here
}

EDIT

Sorry, I did not read your code carefully. Given the status code you get, it seems you need to send an HTTP auth to your server. It would look like that :

String username = "user"
String password = "bitnami"
String userPassword = username + ":" + password;
String encoding = new sun.misc.BASE64Encoder().encode(userPassword.getBytes());

URL url = new URL("http://192.168.0.78/issues.json?key=pjxjDvD9ez0Qm97iApka");

HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Authorization", "Basic " + encoding);
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");

Note : No promise that it will compile/work :)

Code taken from this SO thread : "How to handle HTTP authentication using HttpURLConnection?"

Community
  • 1
  • 1
basgys
  • 4,320
  • 28
  • 39
  • i changed URL to URL url = new URL("http://user:bitnami@192.168.0.78/issues.json"); and i got 422 error :(. Never ending story :((. Do you have any idea?? – Karaban Oct 21 '15 at 22:11
  • @Karaban Are you suppose to send a parameter in order to get the list of issues? Because 422 means the request is syntactically correct, but semantically wrong. A missing parameter should not return a 422, but we never knows. – basgys Oct 22 '15 at 14:15