|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Member
Iscritto dal: Apr 2004
Messaggi: 252
|
[java] request GET tramite url
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 Codice:
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();
}
}
}
Codice:
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)
Grazie |
|
|
|
|
|
#2 |
|
Member
Iscritto dal: Apr 2004
Messaggi: 252
|
up
|
|
|
|
|
|
#3 |
|
Senior Member
Iscritto dal: Jan 2001
Città: Milano
Messaggi: 5707
|
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"); |
|
|
|
|
|
#4 |
|
Member
Iscritto dal: Apr 2004
Messaggi: 252
|
Grazie mille
l'ho scoperto giusto giusto ieri sera...era proprio per quel motivo lì ...adesso funziona tutto |
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 23:35.



















