PDA

View Full Version : [java] request GET tramite url


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

orpheus
19-05-2005, 18:21
up

:muro:

kingv
19-05-2005, 23:15
google rifiuta i client che non si identificano come browser, puoi "aggirare" il blocco fingendo di essere un browser a tutti gli effetti:


conn.setRequestProperty("User-agent","Mozilla/5.0");

orpheus
20-05-2005, 08:34
Grazie mille

l'ho scoperto giusto giusto ieri sera...era proprio per quel motivo lì
...adesso funziona tutto