How to send a query to a search engine?

hi,

How can i send a query to a search engines and get the answer back? I'm not very familiar with HTTP programming so can somebody give me a small example of how to do that?

thnax in advance.
CaillouAsked:
Who is Participating?
 
TimYatesConnect With a Mentor Commented:
import java.net.*;
import java.io.*;

public class AltaVista {
    public static void main (String args[])
    {
     try {
         // make connection
          URL url = new URL("http://www.altavista.com/cgi-bin/query?q=" +
                     URLEncoder.encode(args[0]));
         URLConnection connection = url.openConnection();
         connection.setDoInput(true);    
         InputStream in = connection.getInputStream();

         // read reply
         StringBuffer b = new StringBuffer();
         BufferedReader r = new BufferedReader(new InputStreamReader(in));
         String line;
         while ((line = r.readLine()) != null)
          b.append(line);
         String s = b.toString();

         // look for first search result, if any
         if (s.indexOf(">We found 0 results") != -1)
          System.out.println("No results found.");
         else {
          int i = s.indexOf("\"status='")+9;
          int j = s.indexOf("'", i);
          System.out.println("First result: " + s.substring(i, j));
         }
     }
     catch (Exception e) { e.printStackTrace(); }  
    }
}  
0
 
TimYatesCommented:
0
 
CaillouAuthor Commented:
exactly what i wanted...
thanx a lo, you save me a lot of time.
0
All Courses

From novice to tech pro — start learning today.