Set the “user agent” parameter for urlconnection to query Google from Java applications

I tried to get the results from the Java query 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");

However, I don't know how to set the "user agent" parameter above Where do I get the value of the system? I just copied these values from some code I found on the Internet

My system configuration is as follows: @ h_ 404_ 16@Mac OSX 10.8. 3@H_404_16 @Intel i7@H_404_16 @Safari 6.0. 3(8536.28.10)

Solution

If you just want to know why the user agent is set, you can imitate the response that the browser will get. You can use this site

http://www.whatsmyuseragent.com/

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>