如何修改 HttpUrlConnection 的标头

发布于 2024-07-11 20:16:01 字数 307 浏览 3 评论 0原文

我试图稍微改进一下 Java Html 文档,但我遇到了 HttpUrlConntion 的问题。 有一件事是,如果用户代理是 Java VM,某些服务器会阻止请求。 另一个问题是 HttpUrlConnection 未设置 ReferrerLocation 标头字段。 由于几个网站使用这些字段来验证内容是否是从自己的网站访问的,因此我也被阻止在这里。 据我所知,唯一的解决方案是替换 HTTP 协议的 URL 处理程序。 或者有什么办法可以修改默认的HTTP Handler?

Im trying to improve the Java Html Document a little but i'm running into problems with the HttpUrlConntion. One thing is that some servers block a request if the user agent is a Java VM. Another problem is that the HttpUrlConnection does not set the Referrer or Location header field. Since several sites use these fields to verify that the content was accessed from their own site, I'm blocked here as well. As far as I can see the only resolution is to replace the URL handler of the HTTP protocol. Or is there any way to modify the default HTTP Handler?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

兮子 2024-07-18 20:16:01

使用 URL.openConnection 打开 URL。 可以选择转换为 HttpURLConnection。 调用URLConnection.setRequestProperty/addRequestProperty

默认的 User-Agent 标头值是通过 "http.agent" 系统属性设置的。 PlugIn 和 WebStart 允许您设置此属性。

Open the URL with URL.openConnection. Optionally cast to HttpURLConnection. Call URLConnection.setRequestProperty/addRequestProperty.

The default User-Agent header value is set from the "http.agent" system property. The PlugIn and WebStart allow you to set this property.

看海 2024-07-18 20:16:01

如果您使用 Apache HttpClient 来管理您的编程 HTTP 连接,您将获得一个非常有用的 API这使得创建连接(以及可选的失败时自动重新连接)、设置标头、帖子与获取、检索返回内容的便捷方法等等。

If you use Apache HttpClient to manage your programmatic HTTP connectivity you get an extremely useful API which makes creating connections (and optional automatic re-connecting on fail), setting Headers, posts vs gets, handy methods for retrieving the returned content and much much more.

万人眼中万个我 2024-07-18 20:16:01

我解决了我的问题。 我们可以将 header 发送到 application/json 并将 body 作为 json 对象传递。 这简单地解决了问题。

I solved my problem. We can just send the header to application/json and pass the body as a json object. That simply solves the issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文