View Single Post
  #1 (permalink)  
Old October 22nd, 2007, 09:09 AM
jvlaine jvlaine is offline
Registered User
 
Join Date: Oct 2007
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Accessing Google search with Java

I have tried to access Google search using the code below. When I run the program the server returns HTTP response code: 403. I haven't had any problems with other search engines. Does anyone know how to access Google search with programming?

// download text content of URL

import java.net.*;
import java.io.*;

public class Jget
{
  public static void main ( String[] args ) throws IOException
  {
    try
    {
        URL url = new URL("http://www.google.com/search?q=example");

        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
        String str;

        while ((str = in.readLine()) != null)
        {
          System.out.println(str);
        }

        in.close();
    }
    catch (MalformedURLException e) {}
    catch (IOException e) {}
  }
}

Reply With Quote