Server refuses encoded URL from Java application

I'm trying to send an encoded URL using a Java application via HttpURLConnection. The server accepts the httpRequest just fine when there are no percent encoded values (ex.http://10.132.1.1:80/xx?x=in&xt=&s=&d=&de=&a=&p=&o=&MSISDN=639991234567). But once the percent encoded values are added to the URL as the value of a request parameter(ex.x=in&xt=&s=&d=L%3A%0CM%8B%CEh%2F&de=L%3A%0CM%8B%CEh%2F&de=&a=&p=&o=&MSISDN=639991234567), the server rejects it. I tried sending the latter url using a web browser, and the server accepted it. Here's the code im using:

try {

String data = null;

if(argv.length > 0 && argv[0]!=null)

{

data = argv[0];

}

else

{

data = "x=ba&xt=cd&s=CRIS&d=&de=&a=&p=&o=&MSISDN=639990001111";

}

String hostIP = "http://Put hostIP:port here";

URL url = new URL(hostIP + "?" + data);

HttpURLConnection conn = (HttpURLConnection) url.openConnection();

conn.setRequestMethod("GET");

conn.setRequestProperty ("Content-Type","application/octet-stream");

//conn.setUseCaches (false);

//conn.setDefaultUseCaches (false);

//conn.setRequestMethod("POST");

//conn.setDoOutput(true);

//conn.setRequestProperty("Content-Type","UTF-8");

conn.connect();

if(conn.getErrorStream() != null)

{

BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getErrorStream()));

String line;

while ((line = rd.readLine()) != null) {

// Process line...

System.out.println(line);

}

//wr.close();

rd.close();

}

//OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());

//wr.write(data);

//wr.flush();

// Get the response

System.out.println("Response code:" + conn.getResponseCode());

BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));

String line;

while ((line = rd.readLine()) != null) {

// Process line...

System.out.println(line);

}

//wr.close();

rd.close();

} catch (Exception e) {

System.out.println(e);

}

Can you please help me? Thank you very much.

[2322 byte] By [snipeyxa] at [2007-11-27 7:59:20]
# 1
Can you confirm that the query string passed is received without changes by your program?
quittea at 2007-7-12 19:41:22 > top of Java-index,Java Essentials,Java Programming...
# 2

I can't (or if there's a way I don't know how for now). The only way for me to check the query string passed is by checking the server logs. The odd thing about it is that I don't get any response message from the server when I send URLs with percent encoded data. Does that mean that the request didn't reach the server? I also tried doing a POST instead of a GET but the behavior is the same.

snipeyxa at 2007-7-12 19:41:22 > top of Java-index,Java Essentials,Java Programming...
# 3

> I can't (or if there's a way I don't know how for

> now). The only way for me to check the query string

> passed is by checking the server logs.

Do the logs show a correct URL string? What response code does the server log?

One way to check is to print the string received via args[] to a client-side log file. You could then take this string and paste it into the address field of a browser.

> The odd thing about it is that I don't get any response message

> from the server when I send URLs with percent encoded

> data. Does that mean that the request didn't reach

> the server? I also tried doing a POST instead of a

> GET but the behavior is the same.

If there's no log entry (at least a 5xx response code should be visible), it has not reached the server. If there's an entry and you just don't get a response, it seems as if there was a protocol error giving your web server a bad time ...

quittea at 2007-7-12 19:41:23 > top of Java-index,Java Essentials,Java Programming...
# 4

Here's a sample of the url being sent:

http://10.128.1.23:80/sm?x=set&xt=wpin&s=&d=L%3A%0CM%8B%CEh%2F&de=L%3A%0CM%8B%CEh%2F&a=&p=&o=

the host IP can be ping-ed/telnet(ted?) from the computer where the application is running.

I tried sending the URL via a browser and the server accepted it.

If I send the URL using a servlet, will it make a difference?

snipeyxa at 2007-7-12 19:41:23 > top of Java-index,Java Essentials,Java Programming...
# 5
What does rejected mean?What is the exact error code you are getting. This is important.
cotton.ma at 2007-7-12 19:41:23 > top of Java-index,Java Essentials,Java Programming...
# 6

From the client end, no error code is received. I get a "Connection Timeout" after some time. I'm currently logging the response by 'System.out.println("Response code:" + conn.getResponseCode());'. After doing the conn.connect method, if the url has encoded values, no response code is returned. I also tried the conn.getErrorStream() method but nothing is appearing at all.

snipeyxa at 2007-7-12 19:41:23 > top of Java-index,Java Essentials,Java Programming...
# 7
New developments. I tried running the client application via the Windows console (cmd window) and it works for URLs with or without encoded data. Perhaps the environment where the application is ran has something to do with it.
snipeyxa at 2007-7-12 19:41:23 > top of Java-index,Java Essentials,Java Programming...