Http Connection

H

Hector

Hi all,

I try to read a file from an URL. My code is easy: I open the
connection, send the params requested and get the data stream (from a
XML file).

Everything is ok when I run it into my local Tomcat (5.0.24) (with SDK
1.4.1), but when I execute from remote server I get the following
error: an AccessControlException when I execute the getInputStream
method.

Any help?

Thanks!!!
 
M

Matt Humphrey

Hector said:
Hi all,

I try to read a file from an URL. My code is easy: I open the
connection, send the params requested and get the data stream (from a
XML file).

Everything is ok when I run it into my local Tomcat (5.0.24) (with SDK
1.4.1), but when I execute from remote server I get the following
error: an AccessControlException when I execute the getInputStream
method.

Any help?

As a wild guess, is your code being run as an applet and is the URL pointing
to a machine other than the one where the applet came from? The
AccessControlException is normally thrown for lack of permission, policy
setting or the like.

Cheers,
Matt Humphrey (e-mail address removed) http://www.iviz.com/
 
H

Hector

As a wild guess, is your code being run as an applet and is the URL pointing
to a machine other than the one where the applet came from? The
AccessControlException is normally thrown for lack of permission, policy
setting or the like.

Cheers,
Matt Humphrey (e-mail address removed) http://www.iviz.com/


Thanks Matt,

I'm newbie at this issue. So, I'm trying to get a XML document (via
URL connection to another server where I have an account) through a
JSP page. I need it to update my database. I don't understand very
well the reason (security?) why I can execute it where everithing is
in my local machine (server & database) but not when it's uploaded.

Anyway, any solution for this problem? Or nobody can consult external
servers through a web page?

Best regards for all
 
M

Matt Humphrey

Hector said:
Thanks Matt,

I'm newbie at this issue. So, I'm trying to get a XML document (via
URL connection to another server where I have an account) through a
JSP page. I need it to update my database. I don't understand very
well the reason (security?) why I can execute it where everithing is
in my local machine (server & database) but not when it's uploaded.

Anyway, any solution for this problem? Or nobody can consult external
servers through a web page?

It is possible to refer to an external page from your jsp / servlet. I'm not
familiar with security issues for servlets, so I can't help you very much.
I would check, however, that the place to where you are deploying your code
has the same network structure (it may be behind a firewall that limits
outgoing connections), has an appropriate java security policy and that
you're sure the account / password is valid. If you are deploying it to an
ISP's web app server, they may have restrictions on calling out. (Also,
are you sure the access exception is on the URL and not on the connection to
your database?)

Cheers,
Matt Humphrey (e-mail address removed) http://www.iviz.com/
 
H

Holla

Hector said:
Thanks Matt,

I'm newbie at this issue. So, I'm trying to get a XML document (via
URL connection to another server where I have an account) through a
JSP page. I need it to update my database. I don't understand very
well the reason (security?) why I can execute it where everithing is
in my local machine (server & database) but not when it's uploaded.

Anyway, any solution for this problem? Or nobody can consult external
servers through a web page?

Best regards for all

Few questions.
1. Can access xml file in web browser?
2. Can you share code how you are reading URL....
 
H

Hector

Few questions.
1. Can access xml file in web browser?
2. Can you share code how you are reading URL....

Hi everybody,

1. Yes, I can access my database because I have another code to view
some information from it. In addition to this, I changed my code. I
only try to read the file now (see code below).

2. Yes, I can access xml file with my browser (IE6 or Mozilla
Firefox).

3. My code is simple, because I changed it for a basic class to test:

package prueba;
import java.net.*;
import java.io.*;

public class prueba {
public prueba() throws Exception
{

URL url=new URL("http://www ... file.xml");
HttpURLConnection huc=(HttpURLConnection) url.openConnection();
InputStream is = huc.getInputStream();

}

}

And my index.jsp ...

<%@page contentType="text/html"%>
<%@ page import="prueba.*" %>
<html>
<head><title>JSP Page</title></head>
<body bgcolor="#00CC00">
<%
prueba aplicacion = new prueba();

%>

<center>
<b>Hello</b>
</center>


</body>
</html>


Notes: I also changed the server where I save the XML file to avoid
login process, but the problem is the same.

Thank you very much!
 
H

Hector

I have found in the web other Tomcat server (which it's the same
version that I have (5.0.24)) and my code works ok... Errr, but it
isn't a solution because I need the code working in the first one
(which has a 5.5.7 version). I didn't find any information when I read
the documentation about this topic...

Any suggestion? (I can't run the new Tomcat version on my local
machine, so I can't test my code at home)

Thanks!
 
A

Andrea Desole

I'm not an expert of java security, but maybe you can check a couple of
things:
1) see how Tomcat works with security. Tomcat should have, for example,
its own policy file.
2) try to see if you can get more information from the exception itself.
AccessControlException has a Permission member, take a look to see if
it's set, and what it says
 
H

Holla

Hector said:
Hi everybody,

1. Yes, I can access my database because I have another code to view
some information from it. In addition to this, I changed my code. I
only try to read the file now (see code below).

2. Yes, I can access xml file with my browser (IE6 or Mozilla
Firefox).

3. My code is simple, because I changed it for a basic class to test:

package prueba;
import java.net.*;
import java.io.*;

public class prueba {
public prueba() throws Exception
{

URL url=new URL("http://www ... file.xml");
HttpURLConnection huc=(HttpURLConnection) url.openConnection();

What are you doing here? URL.openConnection returns URLConnection.
Though this may not be causing the problem.
InputStream is = huc.getInputStream();

}

}

And my index.jsp ...

Here few more questions,

1) which is your browser? IE? If browser is IE (internet explorer) it
automatically send logged in domain, user id and password. So you won't see
actual authentication happening.

2) Which is your web server where your XML file is kept?


Below code works perfectly fine for me to fetch any document on the web!

<code>

package test;
import java.io.*;
import java.net.*;
public class HttpTester {
private String path;
public HttpTester(){
this.path="";
}

public HttpTester(String path){
this.path=path;
}

public void setPath(String path){
this.path=path;
}

public String getPath(){
return this.path;
}

public String getContent(){
String content="";
try{
URL url= new URL(this.path);
StringBuffer sb=new StringBuffer();
try{
URLConnection con=url.openConnection();
String s;
boolean shouldRead=true;
InputStream is=con.getInputStream();
BufferedReader br=new BufferedReader(new
InputStreamReader(is));

while(shouldRead){
s=br.readLine();
if(s==null){
shouldRead=false;
}else{
sb.append(s);
sb.append("\n");
}
}
content=sb.toString();
}catch(IOException ioe){
ioe.printStackTrace(System.err);
}
}catch(MalformedURLException me){

}
return content;
}

public static void main(String[] args) {
HttpTester ht=new
HttpTester("http://www.myserver.com/requiredpage/index.html");
System.out.println(ht.getContent());
}
}

</code>


regards
Holla
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,226
Latest member
KristanTal

Latest Threads

Top