Notice: Undefined index: showurlfield in /home2/wikija5/public_html/w/extensions/ArticleComments.php on line 176

Notice: Undefined index: showurlfield in /home2/wikija5/public_html/w/extensions/ArticleComments.php on line 176
Google AutoComplete - WikiJava, google, autocomplete,ajax
Monday, 21st June 2010
The new frontier for learning Java

Google AutoComplete

From WikiJava

Jump to: navigation, search



This page is or contains a derivative version of a portion of this GFDL content
The article was originally sourced from Ganesh Gowtham.

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

Contents

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



Wait for some time google to cache your name (keyword) and have fun :-)

see also

Comments from the users

To be notified via mail on the updates of this discussion you can login and click on watch at the top of the page


Title (required):

Website:

Comment: