对于带有锚点的链接,HttpClient 出现 400 错误
这是我的代码:
DefaultHttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
HttpResponse response = client.execute(request);
这适用于迄今为止我尝试过的每个网址,除了一些包含锚点的网址。其中一些锚定网址返回 400。奇怪的是,并非所有链接都包含锚定,其中很多链接都可以正常工作。
不幸的是,我必须非常笼统,因为我无法在这里提供具体的网址。
这些链接完全有效,并且在任何浏览器中都可以正常工作,但 HttpClient 在尝试该链接时返回 400。如果我移除锚点,它就会起作用。
有什么想法要寻找什么吗?
例如:http://www.somedomain.com/somedirectory/somepage#someanchor
再次对泛型感到抱歉
编辑:我应该提到这是针对 Android 的。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您在网址中使用锚点的方式不正确。
当我们执行“获取”时,我们需要获取整个资源(页面)。锚点只是一个标记位置的标签,通常浏览器会在页面加载后滚动到锚点的位置。在特定锚点“获取”页面是没有意义的 - 必须获取整个页面。
您的结果不一致可能是因为某些网络服务器忽略了锚点组件,而其他服务器正在纠正您的错误。
解决方案只是在运行代码之前删除 url 的 #anchor 部分。
Your usage of the anchor in the url is incorrect.
When we perform a "Get", we need to get the entire resource (page). The anchor is just a tag marking a location, normally your browser will scroll to the position of the anchor once the page is loaded. It does not make sense to "Get" the page at a specific anchor - the entire page must be fetched.
It is possible your inconsistent results are because some web servers are ignoring the anchor component, and others are correcting your error.
The solution is just to remove the #anchor portion of the url before running your code.
正如 @Greg Sansom 所说,URL 不应与锚/片段一起发送。 URL 的片段部分与服务器无关。
以下是来自 HTTP 相关部分的预期 URL 语法1.1 规范:
注意:语法中没有
fragment
部分。如果您确实发送
片段
,那么会发生什么显然是服务器实现特定的。我希望您会看到各种各样的响应:IMO,最明智的解决方案是在实例化
HttpGet
对象之前将其从 URL 中删除。FOLLOWUP
从 URL 字符串中删除片段的推荐方法是将其转换为
java.net.URL
或java.net.URI
实例,提取相关组件,使用它们创建一个新的java.net.URL
或java.net.URI
实例(当然省略片段),最后将其变回字符串。但我认为,如果您可以安全地假设您的 URL 都是有效的绝对 HTTP 或 HTTPS URL,那么以下内容也应该有效。
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
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
orjava.net.URI
instance, extract the relevant components, use these to create a newjava.net.URL
orjava.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.
String user_url2="uhttp://www.somedomain.com/somedirectory/somepage#someanchor";
结果字符串将显示 url 值
String user_url2="uhttp://www.somedomain.com/somedirectory/somepage#someanchor";
result string will display url value
Android HttpClient 中存在一个错误,已在 HttpClient 1.2 中修复,但未向后移植到 Android
https:// /issues.apache.org/jira/browse/HTTPCLIENT-1177
https://github .com/apache/httpclient/commit/be6347aef0f7450133017b775113a8f3fadd2f1c
我已在以下位置打开错误报告:
https://code.google.com/p/android/issues/detail ?id=65909
There is a bug in Android HttpClient that was fixed in HttpClient 1.2 but not backported to Android
https://issues.apache.org/jira/browse/HTTPCLIENT-1177
https://github.com/apache/httpclient/commit/be6347aef0f7450133017b775113a8f3fadd2f1c
I have opened a bug report at:
https://code.google.com/p/android/issues/detail?id=65909