• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Url connection

 
D Dave
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks!!

I am back to java after many years and now I am a bit stalling with this problem.
I am working with a struts 2 + spring + hibernate application, i have a simple action that answer a json just calling: http://localhost:8080/mySite/myJson.action

Now I need to call this url inside another application, but something is wrong - I need to do the authentication but doesn't work:


URL myURL = new URL("http://localhost:8080/mySite/myJson.action");
URLConnection myURLConnection = myURL.openConnection();
myURLConnection.connect();
String userpass = "username" + ":" + "password*";
String basicAuth = "Basic " + new String(new Base64().encode(userpass.getBytes()));
myURLConnection.setRequestProperty("Authorization", basicAuth);
BufferedReader in = new BufferedReader(new InputStreamReader(
(InputStream) myURLConnection.getContent()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
Thanks,
Dave
 
Dave Tolls
Ranch Hand
Posts: 2272
23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you getting any errors?
I do notice you are calling connect() and then setting properties on the connection.
This is backwards.
The connect() call actually makes the request, and setting anything after that won't have any effect on the request.
In fact, it might actually be an error.
 
D Dave
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank Dave! You are right, now I am using this code:

URL myURL = new URL("http://localhost:8080/mysite/json.action");
String userpass = "username" + ":" + "password";
String basicAuth = "Basic " + new String(new Base64().encode(userpass.getBytes()));
URLConnection myURLConnection = myURL.openConnection();

myURLConnection.setRequestProperty("Authorization", "Basic " + basicAuth);
myURLConnection.connect();

BufferedReader in = new BufferedReader(new InputStreamReader(
(InputStream) myURLConnection.getContent()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();

But I am getting the html source code page and not the json that I get if I write the code directly in the url brower: http://localhost:8080/mysite/json.action")

Thanks,
Dave
 
Dave Tolls
Ranch Hand
Posts: 2272
23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what html are you getting?
It might be an error page of some sort.
 
D Dave
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am getting something like :

"<!DOCTYPE>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=iso-8859-1" />
<title>
myapp | </title>
<link rel="SHORTCUT ICON" href="img/favicon.ico" />
<link rel="stylesheet" href="css/overcast/jquery-ui-1.8.20.custom.css" type="text/css" media="screen" charset="utf-8" />
<link rel="stylesheet" href="css/960.css" type="text/css" media="screen" charset="utf-8" />
<link rel="stylesheet" href="css/template.css" type="text/css" media="screen" charset="utf-8" />
<link rel="stylesheet" href="css/colour.css" type="text/css" media="screen" charset="utf-8" />

.....
....."

without errors, instead something like :
"[{"id":2,"dataCreazione":1449151852000,"dataModifica":1449151852000,"nome":"name1","color":"#FFFF4D"},{"id":3,"dataCreazione":1449151852000,"dataModifica":1449151852000,"nome":"name2","color":"#FFFF4D"},{"id":86,"dataCreazione":1449151852000,"dataModifica":1449151852000,"nome":"name3","color":"white"},
.....
...."

Thanks!!
Dave
 
Dave Tolls
Ranch Hand
Posts: 2272
23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you debug the action?
See what it's doing?

Something is returning that html.
 
Joe Ess
Bartender
Posts: 9361
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens if you hit the same URL with a web browser? Do you get HTML or JSON?
 
D Dave
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I hit the same URL with a web browser I got the correct json - inside the program I got this warning in the console "user not authenticated, sending to login...", the problem is clear but the code I am using is this:

String userpass = "nomeUtente" + ":" + "password";
String encoding = new sun.misc.BASE64Encoder().encode (userpass.getBytes());
URLConnection myURLConnection = myURL.openConnection();
myURLConnection.setRequestProperty("Authorization", "Basic " + encoding);

Can be a problem related with the spring framework?

Thanks,
Dave
 
Joe Ess
Bartender
Posts: 9361
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
D Dave wrote:If I hit the same URL with a web browser I got the correct json - inside the program I got this warning in the console "user not authenticated, sending to login...",


That's a good sign. At least the app works.
What kind of authentication does your application require? If it isn't "basic", your code will not work. If you are using typical Java web app security, there will be login-config section in your web.xml. Spring has its own security api. Are you making use of that?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!