Search code examples
javaurl-encodingurlconnection

How to post with java without urlencoding query part of url


I am trying to use a signed java applet to post to a url like:

http://some.domain.com/something/script.asp?param=5041414F9015496EA699F3D2DBAB4AC2|178411|163843|557|1|1|164||attempt|1630315

But when java makes the connection, the java console shows:

network: Connecting http://some.domain.com/something/script.asp?param=5041414F9015496EA699F3D2DBAB4AC2%7C178411%7C163843%7C557%7C1%7C1%7C164%7C%7Cattempt%7C1630315

I do not want java to urlencode the pipes in the query from | to %7c. It seems the service I'm connecting to doesn't urldecode the param, and I can't change the server side code. Is there a way in java to make the post without escaping the query?

The java I'm using is below:

try {
        URL url = new URL(myURL);
        URLConnection connection = url.openConnection();
        connection.setDoOutput(true);

        OutputStreamWriter out = new OutputStreamWriter(
                connection.getOutputStream());
        out.write(toSend);
        out.close();

        BufferedReader in = new BufferedReader(
                new InputStreamReader(
                        connection.getInputStream()));

        String decodedString = "";

        while ((decodedString = in.readLine()) != null) {
            totalResponse = totalResponse + decodedString;
        }
        in.close();

    } catch (Exception ex) {
    }

Thank you for any help!


Solution

  • the URL class does not do any encoding. testing this on my dev server confirmed this suspicion. your code must be encoding the '|' character somewhere before the snippet you included in your question.