Here is my code:
DefaultHttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
HttpResponse response = client.execute(request);
This works for every url I have tried so far, except for some urls that contain an anchor. Some of these anchored urls return a 400. The weird thing is that it isn’t all links that contain an anchor, a lot of them work just fine.
Unfortunately, I have to be really general as I can’t provide the specific urls here.
The links are completely valid and work just fine in any browser, but the HttpClient returns a 400 when trying the link. If I remove the anchor it will work.
Any ideas what to look for?
For example: http://www.somedomain.com/somedirectory/somepage#someanchor
Sorry again for the generics
EDIT: I should mention this is for Android.
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:
Note: there is no
fragmentpart in the syntax.What happens if you do send a
fragmentclearly 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
HttpGetobject.FOLLOWUP
The recommended way to remove a fragment from a URL string is to turn it into a
java.net.URLorjava.net.URIinstance, extract the relevant components, use these to create a newjava.net.URLorjava.net.URIinstance (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.