设置“用户代理".用于从Java应用程序查询Google的URLConnection参数 [英] Setting "User-Agent" parameters for URLConnection for querying Google from a Java application

查看:251
本文介绍了设置“用户代理".用于从Java应用程序查询Google的URLConnection参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试从Java的Google查询中获取结果,如下所示:

I am trying to get the results back from a Google query, in Java as follows:

String urlquery = "https://www.google.com/search?hl=en&gl=us&tbm=nws&q=apples&oq=apples";
URL url = new URL(urlquery);
URLConnection connection = url.openConnection();
URLConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_8; en-US) AppleWebKit/532.5 (KHTML, like Gecko) Chrome/4.0.249.0 Safari/532.5");

但是,我不知道如何设置上面的"User-Agent"参数.从哪里可以获取系统的值?我只是从网上找到的一些代码中复制了这些值.

But, I don't know how to set the "User-Agent" parameters above. From where do I get the values for my system? I just copied these values from some code I found online.

我的系统配置如下: Mac OSX 10.8.3 英特尔i7 Safari 6.0.3(8536.28.10)

My system configuration is as follows: Mac OSX 10.8.3 Intel i7 Safari 6.0.3(8536.28.10)

推荐答案

如果您只想知道将用户代理设置为什么,以便模仿浏览器将获得的响应,则可以使用此站点.

If you just want to know what to set your user agent to so you can mimic the response that browser will get you can use this site.

http://www.whatsmyuseragent.com/

这篇关于设置“用户代理".用于从Java应用程序查询Google的URLConnection参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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