如何修改 HttpUrlConnection 的标头 [英] How to modify the header of a HttpUrlConnection
问题描述
我正在尝试稍微改进 Java Html 文档,但我遇到了 HttpUrlConntion
的问题.一件事是,如果用户代理是 Java VM,则某些服务器会阻止请求.另一个问题是 HttpUrlConnection
没有设置 Referrer
或 Location
标头字段.由于多个站点使用这些字段来验证内容是否是从他们自己的站点访问的,因此我也被阻止在此处.据我所知,唯一的解决办法是更换 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?
推荐答案
使用 URL.openConnection
打开 URL
.可选择转换为 HttpURLConnection
.调用 URLConnection.setRequestProperty
/addRequestProperty
.
Open the URL
with URL.openConnection
. Optionally cast to HttpURLConnection
. Call URLConnection.setRequestProperty
/addRequestProperty
.
默认的 User-Agent 标头值是从 "http.agent"
系统属性设置的.PlugIn 和 WebStart 允许您设置此属性.
The default User-Agent header value is set from the "http.agent"
system property. The PlugIn and WebStart allow you to set this property.
这篇关于如何修改 HttpUrlConnection 的标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!