Hi , this is my code and when I execute it it generates following errors what is wrong with it.
code:
package googleSearch;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
public class linkDiscovery {
private String query = "news agency";
// private String collection = " site : Wikipedia.org";
// private String searchItem = query+collection;
private String searchEngine = "http://www.google.com";
// to search the query in a nominated collection
public void displayItems() throws IOException {
URL url = new URL(searchEngine + "/search?hl=en&source=hp&q="+ URLEncoder.encode(query, "UTF-8")+ "&meta=&aq=f&aqi=g10&aql=&oq=&gs_rfai=");
HttpURLConnection httpConnection = (HttpURLConnection)url.openConnection();
InputStream inputStream = httpConnection.getInputStream();
int ch;
while ((ch=inputStream.read()) > 0) {
System.out.print((char)ch);
}
}
public static void main(String[] args) throws IOException {
new linkDiscovery().displayItems();
}
}
Errors:
Exception in thread "main" java.io.IOException: Server returned HTTP response code: 403 for URL: news agency - Google Search
at sun.net.http://www.protocol.http.HttpURLConn...Stream(Unknown Source)
at googleSearch.linkDiscovery.displayItems(linkDiscov ery.java:28)
at googleSearch.linkDiscovery.main(linkDiscovery.java :38)