tags:

views:

129

answers:

2

I try to talk to a server, by telneting to it, and send the following command through telnet terminal :

POST /%5bvUpJYKw4QvGRMBmhATUxRwv4JrU9aDnwNEuangVyy6OuHxi2YiY=%5dImage? HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Content-Length: 164

[SORT]=0,1,0,10,5,0,KL,0&[FIELD]=33,38,51,58,68,88,78,98,99,101,56,57,69,70,71,72,89,90,91,92,59,60,61,62,79,80,81,82&[LIST]=1155.KL,1295.KL,7191.KL,0097.KL,2267.KL

This works very fine. Now, I wish I can use HttpClient, to talk to the server, as I use telnet to talk to the server. The reason I wish to use HttpClient, instead of using raw TCP socket, is because HttpClient does support NTLM.

However, when I use POST method with NameValuePair :

new NameValuePair("[SORT]", "0,1,0,10,5,0,KL,0")

The request will become URL encoded. The server doesn't understand URL encoded request.

%5BSORT%5D: 0%2C1%2C0%2C10%2C5%2C0%2CKL%2C0

Is there any way I can avoid this? If not, what is the alternative library I can use? I wish to support NTLM as well.

A: 

It's getting URL encoded because your request formed with HTTPClient may be a GET request instead of a POST and is missing this header:

Content-Type: application/x-www-form-urlencoded

Look for the HTTPClient setting to set the Content-Type header correctly and make sure your request is a POST, not a get and you should be golden.

Asaph
No. Using post.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); will not work either. It is still being encoded.
Yan Cheng CHEOK
It's because of what ZZ Coder mentioned. You are relying on a malformed POST body. It's not encoded correctly. If you fix your receiving end to deal with a valid POST body, it will work.
Asaph
+1  A: 

As I mentioned in the other thread, this is not even valid HTTP POST. So you can't do it with default post mechanism in HttpClient. You need to make the invalid body yourself and post it.

Assuming you are using HttpClient 3, following code should work,

 HttpClient httpClient = new HttpClient();  
 PostMethod method = new PostMethod(url);  
 String badFormPost = "[SORT]=0,1,0,10,5,0,KL,0&[FIELD]=33,38,51,58,68,88,78,98,99,101,56,57,69,70,71,72,89,90,91,92,59,60,61,62,79,80,81,82&[LIST]=1155.KL,1295.KL,7191.KL,0097.KL,2267.KL";
 RequestEntity entity = new StringRequestEntity(badFormPost, 
   "application/x-www-form-urlencoded", "UTF-8");
 method.setRequestEntity(entity);
 method.setContentChunked(false);
 httpClient.executeMethod(method);
            ...
ZZ Coder
I just decide to give up and use URLConnection. You suggestion save me :)
Yan Cheng CHEOK