Sending HTML Form, Reading Returned XML data, Special Character Problem

Discussion in 'Java' started by quentinmacdougall@gmail.com, Oct 27, 2005.

  1. Guest

    I am stumped. I have a simple index.html page that contains an HTML
    form and sends data to a CGI script. It works as expected.

    I tried re-recating this index.html FORM as a small Java program. It
    almost works, however, special XML characters are not being encoded
    from my Java program where they are encoded when I run my index.html
    page.

    What am I doing wrong in my java code?

    index.html
    ----------------------------------------------------------------------
    <html>
    <body>
    <FORM NAME="MyTest" ACTION="https://localhost/servlet/SearchUser>
    <INPUT TYPE="hidden" NAME="firstname" VALUE="jack+%26+jill">
    <INPUT TYPE="hidden" NAME="lastname" VALUE="">
    </FORM>
    <SCRIPT LANGUAGE="JavaScript" >
    document.forms[0].submit()
    </SCRIPT>
    </body>
    </html>

    (You see, I encode the ampersand with %26 in the firstname field).
    When I run the above index.html file in my browser, the SearchUser CGI
    script returns the following XML:

    <search>
    <request>
    <lastname></lastname>
    <firstname>Jack+%26+Jill</firstname>
    </request>
    <total>0</total>
    </search>



    PostFormTest.java
    ---------------------------------------------------------
    public class FormPostTest
    {

    public static void
    main(String args[]) throws Exception
    {
    String query = "&firstname=jack+%26+jill&lastname=";
    java.net.URL url = new
    java.net.URL("https://localhost/servlet/SearchUser");

    java.net.HttpURLConnection connection =
    (java.net.HttpURLConnection)url.openConnection();
    connection.setRequestMethod("POST");
    connection.setUseCaches(false);
    connection.setDoInput(true);
    connection.setDoOutput(true);
    connection.setRequestProperty("Content-Type",
    "application/x-www-form-urlencoded");
    connection.setRequestProperty("Content-length",
    String.valueOf(query.length()));
    //connection.connect();

    java.io.DataOutputStream printout = new
    java.io.DataOutputStream (connection.getOutputStream());
    printout.writeBytes (query);
    printout.flush();
    printout.close();

    java.io.InputStreamReader reader = new
    java.io.InputStreamReader(connection.getInputStream(), "ISO-8859-1");
    java.io.BufferedReader in = new java.io.BufferedReader(reader);
    StringBuffer dataBuffer = new StringBuffer();
    String line;

    while((line = in.readLine()) != null)
    {
    dataBuffer.append(line);
    }

    System.out.println("returned: " + dataBuffer.toString());
    }
    }


    When I run this code, I get the following output:

    returned: <search><request><lastname></lastname><firstname>Jack &
    Jill</firstname>
    </request><total>0</total></search>

    You see, the ampersand is not encoded. I would have expected my Java
    code to have the same results as my HTML form code. Am I doing
    something wrong, or is the web browser doing something else I am not
    aware of?
    , Oct 27, 2005
    #1
    1. Advertising

  2. Roedy Green Guest

    On 27 Oct 2005 10:03:43 -0700, wrote,
    quoted or indirectly quoted someone who said :

    >java.net.URL("https://localhost/servlet/SearchUser");


    IIRC your problem is that when you do this in the browser, some
    browsers handle the https for you, but that is not sufficient for a
    standalone Java program. With Java 1.4.1+ SSL is builtin via JSSE Java
    Secure Socket Extension. See the javax.net.ssl class.

    Check out http://mindprod.com/jgloss/ssl.html

    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Oct 27, 2005
    #2
    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. =?Utf-8?B?UHVzaHBlbmRyYQ==?=

    Error reading special character '{' hexadecimal value 0x7B in xml

    =?Utf-8?B?UHVzaHBlbmRyYQ==?=, Jul 24, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    4,552
    Shan Plourde
    Jul 27, 2004
  2. =?Utf-8?B?T3plcg==?=

    Form data special character problem

    =?Utf-8?B?T3plcg==?=, Jul 13, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    593
    =?Utf-8?B?T3plcg==?=
    Jul 18, 2005
  3. KFC
    Replies:
    1
    Views:
    627
  4. KwikRick
    Replies:
    1
    Views:
    349
    Christos TZOTZIOY Georgiou
    Aug 22, 2003
  5. Vijay Pandey

    Data returned by WEB Service contains special characters

    Vijay Pandey, Jun 12, 2006, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    190
    Vijay Pandey
    Jun 12, 2006
Loading...

Share This Page