views:

796

answers:

3

I would like to send a html string with a GET request like this with Apaches HttpClient:

http://sample.com/?html=<html><head>...

This doesnt work at the moment, i think its an encoding problem. Do you have any ideas how to do that?

method.setQueryString(new NameValuePair[] {new NameValuePair("report", "<html>....")});
client.executeMethod(method)

This fails with org.apache.commons.httpclient.NoHttpResponseException: The server localhost failed to respond. If i replace "<html>" by "test.." it works fine.

EDIT

It seams to be a problem of URL length after encoding, the server doesnt except such long URls. Sending it as POST solves the problem.

+3  A: 

Try using URL Encoding to format your html string first.

String yourHtmlString = java.net.URLEncoder.encode("<html>....");
method.setQueryString(new NameValuePair[] {new NameValuePair("report", yourHtmlString)});
Daan
Thanks. Now the string looks encoded, but the server doenst respond to it. Trying the same in the browser manually doenst do anything. Is there an url length limit common in webservers?
Mork0075
@Mork0075 The HTTP specification does not mention length limits, but if you google around a bit, you find that browser and server implementation impose different length limits, so it certainly is something to take into consideration.
Daan
I did some delta debugging and after cropping the string to something around 2000 characters it works. But i would to like store information on the server which exceeds this. Hava you got any ideas?
Mork0075
Solved, sending it with a POST requests does the trick. Thanks
Mork0075
As you're effectively submitting information (i.e. making a change to the server), you should be using POST anyway.
belugabob
+1  A: 

HTML strings contain characters that should be URL encoded. Read here.

You could do the encoding with UrlUtils.simpleFormUrlEncode

kgiannakakis
+2  A: 

I'd go with base64 encoding and maybe some sort of compression before it depending on the length of your content given:

RFC 2068 states: Servers should be cautious about depending on URI lengths above 255 bytes, because some older client or proxy implementations may not properly support these lengths. The spec for URL length does not dictate a minimum or maximum URL length, but implementation varies by browser. On Windows: Opera supports ~4050 characters, IE 4.0+ supports exactly 2083 characters, Netscape 3 -> 4.78 support up to 8192 characters before causing errors on shut-down, and Netscape 6 supports ~2000 before causing errors on start-up.

MahdeTo