Google AutoCompleteFrom WikiJava
In this Article we see how can we request the Google to return us the webpages based on our Keyword . In a tradional way we search Google in Browser , Here we see the same via Java Code Infact the best to search the Google is to use google api key Google api Trick to make appear your name in Google Autocomplete
the code/** * @author Ganesh Gowtham * @url http://sites.google.com/site/ganeshgowtham * Proxy class (HttpAuthenticateProxy) needs to be used * when you are using the internet using the proxy authetication */ class HttpAuthenticateProxy extends Authenticator { String userName = null; String password = null; public HttpAuthenticateProxy(String userName, String password) { this.userName = userName; this.password = password; } protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(userName, password.toCharArray()); } }
/** * @author Ganesh Gowtham * @url http://sites.google.com/site/ganeshgowtham * Proxy class (HttpAuthenticateProxy) needs to be used * when you are using the internet using the proxy authetication * http.proxyHost- Proxy Server Name * http.proxyPort- Proxy Port Number * proxyuserName - Proxy Server UserName * proxyPwd - Proxy Server Password */ public class GoogleSearchExample { public static void main(String[] args) throws Exception { Authenticator.setDefault(new HttpAuthenticateProxy("proxyuserName", "proxyPwd")); System.setProperty("http.proxyHost", "proxyServer"); System.setProperty("http.proxyPort", "8080"); while (true) { String link = "http://www.google.co.in/search?q=ganesh+gowtham"; URL url = new URL(link); URLConnection urlConnection = url.openConnection(); urlConnection.setRequestProperty( "User-Agent", "Mozilla/5.0 (X11; U; Linux x86_64; en-GB; rv:1.8.1.6) Gecko/20070723 Iceweasel/2.0.0.6 (Debian-2.0.0.6-0etch1)"); BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); String inputLine; while ((inputLine = in.readLine()) != null) { System.out.println(inputLine); } in.close(); } } } Below code is necessary as without the "User-Agent" Google will return us the java.io.IOException: Server returned HTTP response code: 403 Hence in the below code we try to simulate and request as if we are sending from Browser ( here i used Mozilla ) Above code i used while(true) which means it will go and end-up in infinite loop keeps pinging Google server , So that you can see your name in Google Auto complete in near future . urlConnection.setRequestProperty( "User-Agent", "Mozilla/5.0 (X11; U; Linux x86_64; en-GB; rv:1.8.1.6) Gecko/20070723 Iceweasel/2.0.0.6 (Debian-2.0.0.6-0etch1)");
see also |
