HttpURLConnection to Windows vs Lunux problems

D

David G

Hello, I have run across a really weird situation with the
HttpURLConnection. I always seem to get the following error when connecting
(via HttpURLConnection) to a lunux tomcat servlet engine:
java.io.IOException: Server returned HTTP response code: 500 for URL:
http://venus/david/utils/formTester.jsp?url=testurl&

However, when I make a connection to the exact same page on a windows tomcat
it works.

This is what I'm seeing. When ever I request a static page from tomcat (on
linux), it works fine, but if I request a jsp it breaks with the 500 error
above.
For example:
venus is a linux server and davidlg is my development windows 2000

pt.doGet("http://venus/index.html"); // returns the html page
pt.doGet("http://venus:8080/david/utils/password.jsp"); // returns error
500
pt.doGet("http://venus:8080/david/utils/formTester.jsp?url=testurl"); //
returns error 500

pt.doGet("http://davidlg:8084/index.html"); // returns html
pt.doGet("http://davidlg:8084/apps/utils/password.jsp"); // returns
generated web page (simple page that looks for cgi parm and prints it)
pt.doGet("http://davidlg:8084/apps/utils/formTester.jsp?url=testurl");//
returns generated web page (another simple page that prints a form with a
cgi parm in it)


Anybody have any ideas? This is driving me nuts...

Here is my code that makes the connection:

public String doGet(String urlString, Hashtable parms)
throws ProtocolException, IOException, UnsupportedEncodingException {

String name;
String value;
// make sure there is a ? between the page and parms.
Enumeration e = parms.keys();
if (urlString.indexOf("?") < 0) {
urlString += "?";
} else if (urlString.indexOf("?") != urlString.length() - 1) {
urlString += "&";
}

boolean useDepracated = false;
log("java.version:" + System.getProperty("java.version"));
useDepracated =
(System.getProperty("java.version").indexOf("1.2")) == 0;
if (useDepracated) {
log(
"Found earlier version of java (1.2.2), using the"
+ " depricated version of url encode:");
}
while (e.hasMoreElements()) {
name = (String)e.nextElement();
value = (String)parms.get(name);
// encode the value:

if (useDepracated) {
value = URLEncoder.encode(value);
} else {
value = URLEncoder.encode(value, "UTF-8");
}
urlString += name + "=" + value + "&";
log(name + "=" + value + "&");
}
log("Sending the following to " + urlString);
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("GET");
conn.setDoOutput(true);

PrintWriter out = new PrintWriter(conn.getOutputStream());

out.println();
out.flush();
out.close();

// read the response:
//conn.connect();
conn.getResponseMessage(); // OK, Forbidden, etc
conn.getResponseCode(); // 200, 404, etc
// this is where it throws the 500 error.
BufferedReader in =
new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuffer response = new StringBuffer();
String line;
while ((line = in.readLine()) != null) {
log(line);
response.append(line);
}
in.close();
return response.toString();
}


Thanks for any suggestions you have to offer.
-David
 
D

David G

Well, I think I found out how to fix it, but I still don't understand the
root of the problem
See below for fix:

David G said:
Hello, I have run across a really weird situation with the
HttpURLConnection. I always seem to get the following error when connecting
(via HttpURLConnection) to a lunux tomcat servlet engine:
java.io.IOException: Server returned HTTP response code: 500 for URL:
http://venus/david/utils/formTester.jsp?url=testurl&

However, when I make a connection to the exact same page on a windows tomcat
it works.

This is what I'm seeing. When ever I request a static page from tomcat (on
linux), it works fine, but if I request a jsp it breaks with the 500 error
above.
For example:
venus is a linux server and davidlg is my development windows 2000

pt.doGet("http://venus/index.html"); // returns the html page
pt.doGet("http://venus:8080/david/utils/password.jsp"); // returns error
500
pt.doGet("http://venus:8080/david/utils/formTester.jsp?url=testurl"); //
returns error 500

pt.doGet("http://davidlg:8084/index.html"); // returns html
pt.doGet("http://davidlg:8084/apps/utils/password.jsp"); // returns
generated web page (simple page that looks for cgi parm and prints it)
pt.doGet("http://davidlg:8084/apps/utils/formTester.jsp?url=testurl");//
returns generated web page (another simple page that prints a form with a
cgi parm in it)


Anybody have any ideas? This is driving me nuts...

Here is my code that makes the connection:

public String doGet(String urlString, Hashtable parms)
throws ProtocolException, IOException, UnsupportedEncodingException {

String name;
String value;
// make sure there is a ? between the page and parms.
Enumeration e = parms.keys();
if (urlString.indexOf("?") < 0) {
urlString += "?";
} else if (urlString.indexOf("?") != urlString.length() - 1) {
urlString += "&";
}

boolean useDepracated = false;
log("java.version:" + System.getProperty("java.version"));
useDepracated =
(System.getProperty("java.version").indexOf("1.2")) == 0;
if (useDepracated) {
log(
"Found earlier version of java (1.2.2), using the"
+ " depricated version of url encode:");
}
while (e.hasMoreElements()) {
name = (String)e.nextElement();
value = (String)parms.get(name);
// encode the value:

if (useDepracated) {
value = URLEncoder.encode(value);
} else {
value = URLEncoder.encode(value, "UTF-8");
}
urlString += name + "=" + value + "&";
log(name + "=" + value + "&");
}
log("Sending the following to " + urlString);
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection)url.openConnection(); From here .......
conn.setRequestMethod("GET");
conn.setDoOutput(true);

PrintWriter out = new PrintWriter(conn.getOutputStream());

out.println();
out.flush();
out.close();

.........to here was take out, and then it seemed to work fine...
I though I would want to flush everything, especially if it was a really
long url (like one with a bunch or cgi parms in it..)
 
S

Sudsy

David said:
Well, I think I found out how to fix it, but I still don't understand the
root of the problem
From here .......



........to here was take out, and then it seemed to work fine...
I though I would want to flush everything, especially if it was a really
long url (like one with a bunch or cgi parms in it..)

You only need that code if your method is POST. And then you
also have to URLEncode the query string which is sent as the
body. Since you're just doing a GET then it's superfluous. I
should mention that GET is the default and you could have
snipped at the setDoOutput line...
 
D

David G

Thanks for the explanation. That makes sense. Any idea why it would work
with a windows server and not a Linux server?...My guess would be that the
Linux is more stringent with the http request than a windows environment???
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top