如何修改HttpUrlConnection的标头 [英] How to modify the header of a HttpUrlConnection

查看:186
本文介绍了如何修改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 with URL.openConnection 。可选择强制转换为 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆