设置 java URLConnection 的用户代理 [英] Setting user agent of a java URLConnection

查看:30
本文介绍了设置 java URLConnection 的用户代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用带有 URLConnection 的 Java 解析网页.我尝试像这样设置用户代理:

I'm trying to parse a webpage using Java with URLConnection. I try to set up the user-agent like this:

java.net.URLConnection c = url.openConnection();
c.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");

但是生成的用户代理是我指定的,在末尾附加了Java/1.5.0_19".有没有办法在没有这个添加的情况下真正设置用户代理?

But the resulting user agent is the one I specify, with "Java/1.5.0_19" appended to the end. Is there a way to truly set the user agent without this addition?

推荐答案

副手,将 http.agent 系统属性设置为 "" 可能会解决问题(我没有代码在我面前).

Off hand, setting the http.agent system property to "" might do the trick (I don't have the code in front of me).

你可能会逃脱:

 System.setProperty("http.agent", "");

但这可能需要您和 URL 协议处理程序的初始化之间进行竞争,如果它在启动时缓存该值(实际上,我认为不会).

but that might require a race between you and initialisation of the URL protocol handler, if it caches the value at startup (actually, I don't think it does).

也可以通过 JNLP 文件(可用于 6u10 的小程序)和在命令行上设置该属性:

The property can also be set through JNLP files (available to applets from 6u10) and on the command line:

-Dhttp.agent=

或者对于包装器命令:

-J-Dhttp.agent=

这篇关于设置 java URLConnection 的用户代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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