HttpURLConnection to Windows vs Lunux problems

Discussion in 'Java' started by David G, Dec 15, 2003.

  1. David G

    David G Guest

    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
    David G, Dec 15, 2003
    #1
    1. Advertising

  2. David G

    David G Guest

    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" <david_no_@the-con_spam_nection_please_.com> wrote in message
    news:3fde347a$0$41296$...
    > 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..)

    >
    > // 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
    >
    >
    David G, Dec 15, 2003
    #2
    1. Advertising

  3. David G

    Sudsy Guest

    David G wrote:
    > Well, I think I found out how to fix it, but I still don't understand the
    > root of the problem

    <snip>
    > 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..)


    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...
    Sudsy, Dec 16, 2003
    #3
  4. David G

    David G Guest

    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???

    "Sudsy" <> wrote in message
    news:...
    > David G wrote:
    > > Well, I think I found out how to fix it, but I still don't understand

    the
    > > root of the problem

    > <snip>
    > > 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..)

    >
    > 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...
    >
    David G, Dec 16, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Flip
    Replies:
    8
    Views:
    673
  2. Siegfried Ertl

    HttpURLConnection

    Siegfried Ertl, Nov 30, 2003, in forum: Java
    Replies:
    1
    Views:
    2,252
    Sudsy
    Nov 30, 2003
  3. igoR Buttler
    Replies:
    1
    Views:
    12,131
  4. Niels Campbell

    HttpURLConnection timeout solution

    Niels Campbell, Jan 23, 2004, in forum: Java
    Replies:
    6
    Views:
    43,738
    dadada
    Oct 27, 2006
  5. tony vee

    HttpUrlConnection Problems

    tony vee, Feb 6, 2004, in forum: Java
    Replies:
    2
    Views:
    5,927
    tony vee
    Feb 6, 2004
Loading...

Share This Page