简体   繁体   English

为什么我不能设置Java http User-Agent?

[英]Why can't I set Java http User-Agent?

I tried to set the User Agent for http request like this: 我试图这样设置HTTP请求的用户代理:

public BufferedReader readURL(String url){
        URL urlcon;
        BufferedReader in = null;
        try {
            urlcon = new URL(url);
            connection = (HttpURLConnection)urlcon.openConnection();

            System.setProperty("http.agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0)");
            connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0)");
            System.out.println(connection.getHeaderField("User-Agent"));
            connection.connect();
            in = new BufferedReader(
                                    new InputStreamReader(
                                        connection.getInputStream()));

            String header = connection.getHeaderField(0);
            System.out.println(header);
            System.out.println("---Start of headers---");
            int i = 1;
            while ((header = connection.getHeaderField(i)) != null) {
                String key = connection.getHeaderFieldKey(i);
                System.out.println(((key==null) ? "" : key + ": ") + header);
                i++;
            }
            System.out.println(connection.getHeaderField("http.agent"));
            System.out.println("---End of headers---");
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return in;
    }

And what i got was User-Agent null: 我得到的是User-Agent null:

null 空值
HTTP/1.0 200 OK HTTP / 1.0 200 OK
---Start of headers--- ---标题开始---
Server: Apache 服务器:Apache
Cache-Control: max-age=10 快取控制:max-age = 10
Expires: Sun, 07 Aug 2011 16:09:26 GMT 过期:2011年8月7日,星期日
Vary: Accept-Encoding 有所不同:接受编码
Content-Type: text/html 内容类型:text / html
Content-Length: 163582 内容长度:163582
Date: Sun, 07 Aug 2011 16:09:20 GMT 日期:2011年8月7日,星期日,格林尼治标准时间
X-Varnish: 889692780 889684459 X-清漆:889692780 889684459
Age: 4 年龄:4
Connection: keep-alive 连接:保持活动状态
X-Bip: 889692780 70 148 X线:889692780 70148
Via: 1.1 CachOS 通过:1.1 CachOS
null 空值
---End of headers--- ---标题结尾---

Why can't I set the User-Agent ? 为什么我不能设置用户代理?

使用setHeader()而不是setRequestProperty。

Setting system property: "http.agent" will change your connection header: "User-Agent", but notice that according to documentation your java version is still written in it: 设置系统属性:“ http.agent”将更改您的连接头:“ User-Agent”,但请注意, 根据文档,您的Java版本仍写在其中:

Misc HTTP properties 杂项HTTP属性

http.agent (default: “Java/”) Defines the string sent in the User-Agent request header in http requests. http.agent(默认值:“ Java /”)定义在http请求中的User-Agent请求标头中发送的字符串。

Note that the string “Java/” will be appended to the one provided in the property (eg if -Dhttp.agent=”foobar” is used, the User-Agent header will contain “foobar Java/1.5.0” if the version of the VM is 1.5.0). 请注意,字符串“ Java /”将附加到属性中提供的字符串(例如,如果使用-Dhttp.agent =“ foobar”,则如果版本为User-Agent头将包含“ foobar Java / 1.5.0”) VM的版本为1.5.0)。 This property is checked only once at startup. 启动时仅检查一次此属性。

Notice that property is checked only once at startup, so you may want to first set property, than create your first url and connect. 注意,属性在启动时仅被检查一次,因此您可能要先设置属性,而不是创建第一个URL并进行连接。

To verify that appropriate header is sent, you can use " tcpdump ". 要验证是否发送了适当的头,可以使用“ tcpdump ”。 Usage: 用法:

tcpdump -n dst host stackoverflow.com -vvvv

The server returns the header information. 服务器返回标头信息。 I would guess "User-Agent" isn't important enough to be return. 我猜想“ User-Agent”并不重要,无法返回。 Just because it isn't returned doesn't mean it isn't sent. 仅仅因为它没有被返回并不意味着它没有被发送。

I have a little tool (ieHTTPHeader) that displays the header information. 我有一个显示标题信息的小工具(即HTTPHeader)。 When I do a refresh on this page this it the first set of headers that are sent and returned: 当我对此页面进行刷新时,它是发送和返回的第一组标题:

GET /questions/6973981/why-cant-i-set-java-http-user-agent HTTP/1.1 GET / questions / 6973981 / why-cant-i-set-java-http-user-agent HTTP / 1.1
Accept: / 接受: /
Referer: https://stackoverflow.com/questions/tagged/java?page=2&sort=newest&pagesize=15 引荐来源: https ://stackoverflow.com/questions/tagged/java page = 2& sort = newest pagesize = 15
Accept-Language: en-ca 接受语言:en-ca
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0) 用户代理:Mozilla / 4.0(兼容; MSIE 8.0; Windows NT 5.1; Trident / 4.0)
Accept-Encoding: gzip, deflate 接受编码:gzip,放气
Host: stackoverflow.com 主持人:stackoverflow.com
Connection: Keep-Alive 连接:保持活动
Cookie: __utmc=140029553; Cookie:__utmc = 140029553; __utma=140029553.1370458634.1310761265.1312727448.1312739618.123; __utma = 140029553.1370458634.1310761265.1312727448.1312739618.123; __utmz=140029553.1312739618.123.123.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=yahoo%20finance%20api%20java; __utmz = 140029553.1312739618.123.123.utmcsr =谷歌| utmccn =(有机)| utmcmd =有机| utmctr =雅虎%20finance%20api%20java; __qca=P0-1025379872-1310761265343; __qca = P0-1025379872-1310761265343; m=4; M = 4; usr=t=cEyCYO7bXECF&s=X6DJTj5kuY8H; USR = T = cEyCYO7bXECF&S = X6DJTj5kuY8H; __utmb=140029553.15.10.1312739618 __utmb = 140029553.15.10.1312739618

HTTP/1.1 200 OK HTTP / 1.1 200 OK
Cache-Control: public, max-age=60 缓存控制:公共,最大年龄= 60
Content-Type: text/html; 内容类型:text / html; charset=utf-8 字符集= utf-8的
Content-Encoding: gzip 内容编码:gzip
Expires: Sun, 07 Aug 2011 18:25:43 GMT 过期:星期日,2011年8月7日18:25:43 GMT
Last-Modified: Sun, 07 Aug 2011 18:24:43 GMT 上次修改时间:2011年8月7日,星期日,格林尼治标准时间
Vary: * 变化:*
Date: Sun, 07 Aug 2011 18:24:42 GMT 日期:2011年8月7日,星期日,格林尼治标准时间
Content-Length: 12040 内容长度:12040

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM