psoulos
psoulos

Reputation: 830

How can I use a URL's search engine to return the result of the search?

I've read some tutorials, and I understand what's going on here: http://download.oracle.com/javase/tutorial/networking/urls/readingWriting.html

My question is, how can I use a URL or URLConnection to access the result of a search. For instance, if my URL was:

URL url = new URL("http://www.stackoverflow.com/");     

how could I access the search-engine at the top of the page to return the results of a search?

Upvotes: 1

Views: 1054

Answers (1)

vbence
vbence

Reputation: 20333

You should check the page source. It will contain a <form> element. Its action property will hold the URL of the search script. You also have to send your keywords using the name defined in the <input> tag inside the form.

Using SO as the example:

                <form id="search" action="/search" method="get">

                <div>

                    <input name="q" class="textbox" tabindex="1" onfocus="if (this.value=='search') this.value = ''" type="text" maxlength="140" size="28" value="search">

                </div>

                </form>

Will give the following URL:

http://www.stackoverflow.com/search?q=your+keywords+here

You have to encode your search terms using URL encoding. The very basic thing here is to substitute spaces with the character +.

Upvotes: 1

Related Questions