?
Solved

How to send a query to a search engine?

Posted on 2003-03-30
3
Medium Priority
?
227 Views
Last Modified: 2010-03-31
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.
0
Comment
Question by:Caillou
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 35

Accepted Solution

by:
TimYates earned 500 total points
ID: 8235413
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
 
LVL 35

Expert Comment

by:TimYates
ID: 8235415
0
 

Author Comment

by:Caillou
ID: 8247871
exactly what i wanted...
thanx a lo, you save me a lot of time.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question