Setting “User-Agent” parameters for URLConnection for querying Google from a Java application

帅比萌擦擦* 提交于 2019-12-07 17:49:01

问题


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");

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.

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


回答1:


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/




回答2:


The steps blow can help you to get the "User-Agent":

  1. open the chrome , press the F12.
  2. enter any available web site , then press Enter.
  3. click the "NetWork" on DevTools, and click the web site that entered just right.
  4. so you can find the "User-Agent", just like the picture below. Just like this


来源:https://stackoverflow.com/questions/16026596/setting-user-agent-parameters-for-urlconnection-for-querying-google-from-a-jav

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!