orpheus
18-05-2005, 15:07
Ciao, ho un problema
in pratica devo scrivere un programma che mi permetta di fare una ricerca tramite google.
Questo è il codice che ho scritto
public class Retriever{
public static void main(String[] args){
try {
// Construct data
String data = "hl=" + URLEncoder.encode("it", "UTF-8");
data += "&q=" + URLEncoder.encode("prova", "UTF-8");
data += "&btnG=" + URLEncoder.encode("Cerca", "UTF-8");
data += "&meta=" + URLEncoder.encode("", "UTF-8");
System.out.println(data);
// Send data
URL url = new URL("http://www.google.it/search?"+data);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
System.out.println(line);
}
rd.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Quando tento l'interrogazione mi viene tornato questo errore HTTP nella reponse
java.io.IOException: Server returned HTTP response code: 403 for URL: http://www
.google.it/search?hl=it&q=prova&btnG=Cerca&meta=
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLCon
nection.java:789)
at Retriever.main(Retriever.java:30)
Ho sbagliato qualcosa? Devo scrivere la richiesta in altri modi?
Grazie
in pratica devo scrivere un programma che mi permetta di fare una ricerca tramite google.
Questo è il codice che ho scritto
public class Retriever{
public static void main(String[] args){
try {
// Construct data
String data = "hl=" + URLEncoder.encode("it", "UTF-8");
data += "&q=" + URLEncoder.encode("prova", "UTF-8");
data += "&btnG=" + URLEncoder.encode("Cerca", "UTF-8");
data += "&meta=" + URLEncoder.encode("", "UTF-8");
System.out.println(data);
// Send data
URL url = new URL("http://www.google.it/search?"+data);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
System.out.println(line);
}
rd.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Quando tento l'interrogazione mi viene tornato questo errore HTTP nella reponse
java.io.IOException: Server returned HTTP response code: 403 for URL: http://www
.google.it/search?hl=it&q=prova&btnG=Cerca&meta=
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLCon
nection.java:789)
at Retriever.main(Retriever.java:30)
Ho sbagliato qualcosa? Devo scrivere la richiesta in altri modi?
Grazie