Retrieving Json via HTML request from Jboss server
Posted
by
Seth Solomon
on Stack Overflow
See other posts from Stack Overflow
or by Seth Solomon
Published on 2012-08-28T21:37:09Z
Indexed on
2012/08/28
21:38 UTC
Read the original article
Hit count: 317
I am running into a java.net.SocketException: Unexpected end of file from server when I am trying to query some JSON from my JBoss server. I am hoping someone can spot where I am going wrong. Or does anyone have any suggestions of a better way to pull this JSON from my Jboss server?
try{
URL u = new URL("http://localhost:9990/management/subsystem/datasources/data-source/MySQLDS/statistics?read-resource&include-runtime=true&recursive=true");
HttpURLConnection c = (HttpURLConnection) u.openConnection();
String encoded = Base64.encode(("username"+":"+"password").getBytes());
c.setRequestMethod("POST");
c.setRequestProperty("Authorization", "Basic "+encoded);
c.setRequestProperty("Content-Type","application/json");
c.setUseCaches(false);
c.setAllowUserInteraction(false);
c.setConnectTimeout(5000);
c.setReadTimeout(5000);
c.connect();
int status = c.getResponseCode(); // throws the exception here
switch (status) {
case 200:
case 201:
BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line+"\n");
}
br.close();
System.out.println(sb.toString());
break;
default:
System.out.println(status);
break;
}
} catch (Exception e)
{
e.printStackTrace();
}
© Stack Overflow or respective owner