Question

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.

Was it helpful?

Solution

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)});

OTHER TIPS

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.

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

You could do the encoding with UrlUtils.simpleFormUrlEncode

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top