Here is my code:
DefaultHttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
HttpResponse response = client.execute(request);
As @Greg Sansom says, the URL should not be sent with an anchor / fragment. The fragment part of the URL is not relevant to the server.
Here's the expected URL syntax from relevant part of the HTTP 1.1 specification:
http_URL = "http:" "//" host [ ":" port ] [ abs_path [ "?" query ]]
Note: there is no fragment
part in the syntax.
What happens if you do send a fragment
clearly is server implementation specific. I expect that you will see a variety of responses:
IMO, the most sensible solution is to strip it from the URL before instantiating the HttpGet
object.
FOLLOWUP
The recommended way to remove a fragment from a URL string is to turn it into a java.net.URL
or java.net.URI
instance, extract the relevant components, use these to create a new java.net.URL
or java.net.URI
instance (leaving out the fragment of course), and finally turn it back into a String.
But I think that the following should also work, if you can safely assume that your URLs are all valid absolute HTTP or HTTPS URLs.
int pos = url.indexOf("#");
String strippedUrl = (pos >= 0) ? url.substring(0, pos) : url;