View Full Version : Java - problema scarico pagina da server web
Ciao ragazzi vado al dunque...
Vorrei fare in Java una richiesta di un pagina web e poterla mettere in locale.... e fin qui non ci sono problemi se chiedo una pagina non protetta...
solo che nel mio caso non funziona xkè il server web da cui vorrei scaricare (il mo router), se uso un browser, mi chiede di autenticarmi.
(ovviamente conosco user e pwd). Sembra che in java semplicemente non mi scarichi la pagina...
l'errore viene quando chiamo la
try {stream = new BufferedReader (new InputStreamReader (url.openStream()));}
Penso di dover modificare l'header della request al server ma non so bene come..
qualcuno è in grado?
Grazzie
se usi una release recente di Java puoi estendere la classe java.net.Authenticator overridando il metodo getPasswordAuthentication().
se usi una release vecchia c'e' comunque il modo, posta che vedo di recuperare il codice
La release di jdk è recente (j2sdk1.4.2_02)
Dunque dovrei estendere la mia classe getPage con Java.net.Authenticator e poi ridefinire il metodo getPasswordAuthentication() ma come devo ridefinirlo? Cosa ci metto dentro e come faccio la richiesta?
ipotizzando:
username="username"
password="password"
Come dovrei modificare il listato?
import java.net.*;
import java.io.*;
public class getPage extends Java.net.Authenticator
{
public static void main(String[] arg)
{
String un;
try {un=arg[0];}
catch (ArrayIndexOutOfBoundsException e)
{
un="http://10.0.0.2/doc/ppp.htm";
System.out.println("Nessun URL definito, prendo "+un);
};
System.out.println("URL:"+un);
URL url;
boolean pippo=false;
try {url= new URL(un);}
catch (MalformedURLException e)
{
System.out.println("URL errato, prendo http://10.0.0.2/doc/ppp.htm ");
url = null;
pippo=true;
};
if (pippo) try {url = new URL ("http://10.0.0.2/doc/ppp.htm ");}
catch (MalformedURLException e){System.out.println("MalformedURLException");};
BufferedReader stream;
try {stream = new BufferedReader (new InputStreamReader (url.openStream()));}
catch (IOException e){
System.out.println("Errore di apertura del file URL");
stream=null;
System.exit(0);
};
// è qui che mi catcha l'errore...
File out=new File(".\\"+url.getFile());
FileWriter Output;
try {Output=new FileWriter(out);}
catch (IOException e) {Output=null;};
String l;
try
{
while ((l=stream.readLine())!=null)
{
Output.write(l);
};
Output.flush();
Output.close();
}
catch (IOException e){System.out.println("Errore di lettura.");};
}
}
..ovviamente ti ringrazio per l'auito!!;)
scusa il ritardo, ero malato :O
creati un'altra classe (magari come inner della tua):
class MyAuthenticator extends Authenticator
{
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication ("username", "password");
}
}
poi prima di fare la chiamata remota chiama:
Authenticator.setDefault (new MyAuthenticator ());
dovrebbe funzionare ;) (non ho qui la possibilità di testarlo)
Ci mancherebbe, spero adesso tu sia ok!:cool:
Anke io ci sono passato ed è durata poco x fortuna...
Ho provato con i tuoi nuovi consigli ma mi da errore il compilatore...(anche con la classe dentro l'altra)
Qnd puoi dargli un occhio...e grazie 1000 in anticipo!:rolleyes:
C:\pppoe\getPage.java:8: cannot resolve symbol
symbol : constructor PasswordAuthentication (java.lang.String,java.lang.String)
location: class java.net.PasswordAuthentication
return new PasswordAuthentication("username","password");
^
1 error
___________________
Il codice è questo:
-------------------
import java.net.*;
import java.io.*;
//creati un'altra classe (magari come inner della tua):
class MyAuthenticator extends Authenticator{
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication("username","password");
}
}
//poi prima di fare la chiamata remota chiama:
//Authenticator.setDefault (new MyAuthenticator ());
public class getPage{
public static void main(String[] arg){
String un;
try {un=arg[0];}
catch (ArrayIndexOutOfBoundsException e){
un="http://10.0.0.2/doc/ppp.htm";
System.out.println("Nessun URL definito, prendo "+un);
};
System.out.println("URL:"+un);
URL url;
boolean pippo=false;
try {url= new URL(un);}
catch (MalformedURLException e){
System.out.println("URL errato, prendo http://10.0.0.2/doc/ppp.htm ");
url = null;
pippo=true;
};
if (pippo) try {url = new URL ("http://10.0.0.2/doc/ppp.htm ");}
catch (MalformedURLException e){System.out.println("MalformedURLException");};
Authenticator.setDefault (new MyAuthenticator ());
BufferedReader stream;
try {stream = new BufferedReader (new InputStreamReader (url.openStream()));}
catch (IOException e){
System.out.println("Errore di apertura del file URL");
stream=null;
System.exit(0);
};
File out=new File(".\\"+url.getFile());
FileWriter Output;
try {Output=new FileWriter(out);}
catch (IOException e) {Output=null;};
String l;
try{while ((l=stream.readLine())!=null)
{ Output.write(l); };
Output.flush();
Output.close();
}
catch (IOException e){System.out.println("Errore di lettura.");};
}//end main
}// end class
hai ragione, sostituisci:
class MyAuthenticator extends Authenticator{
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication("username","password");
}
}
con
class MyAuthenticator extends Authenticator{
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication("username", "password".toCharArray());
}
}
naturalmente io ho cablato username e password a codice per fare un esempio, tu li prenderai dove ritieni piu' opportuno ;)
Eccomi di nuovo qui, son stato preso tra 1000 impegni...e purtroppo non ho risolto il mio problema... l'errore che esce ora è una Null pointer exception quando prova scrivere il file...:(
Ti allego il solito sorgente...sembra che apra il file ma che punti a null.. boh!!:confused:
Ti ringrazzzzio in anticipo!
import java.net.*;
import java.io.*;
class MyAuthenticator extends Authenticator{
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication("admin", "password".toCharArray());
}
}
//poi prima di fare la chiamata remota chiama:
//Authenticator.setDefault (new MyAuthenticator ());
public class getPage{
public static void main(String[] arg){
String un;
try {un=arg[0];}
catch (ArrayIndexOutOfBoundsException e){
un="http://10.0.0.2/doc/ppp.htm";
System.out.println("Nessun URL definito, prendo "+un);
};
System.out.println("URL:"+un);
URL url;
boolean pippo=false;
try {url= new URL(un);}
catch (MalformedURLException e){
System.out.println("URL errato, prendo http://10.0.0.2/doc/ppp.htm ");
url = null;
pippo=true;
};
if (pippo) try {url = new URL ("http://10.0.0.2/doc/ppp.htm ");}
catch (MalformedURLException e){System.out.println("MalformedURLException");};
Authenticator.setDefault (new MyAuthenticator ());
BufferedReader stream;
try {stream = new BufferedReader (new InputStreamReader (url.openStream()));}
catch (IOException e){
System.out.println("Errore di apertura del file URL");
stream=null;
System.exit(0);
};
File out=new File(".\\"+url.getFile());
System.out.println(url.getFile());
FileWriter Output;
try {Output=new FileWriter(out);}
catch (IOException e) {Output=null;};
String l;
try{
while ((l=stream.readLine())!=null){Output.write(l);};
Output.flush();
Output.close();
}
catch (Exception e){System.out.println("Errore di lettura.");};
}//end main
}// end class
File out=new File(".\\"+url.getFile());
questa riga non mi piace, sposta la System.out che c'e' sotto una riga sopra questa, riprova e dimmi cosa ti stampa :O
Ti allego solo il codice modificato su tua indicazione:
System.out.println(url.getFile()); /* qui stampa: /doc/ppp.htm */
File out=new File(".\\"+url.getFile());
FileWriter Output;
try {Output=new FileWriter(out);}
catch (IOException e) {Output=null;};
String l;
try{
while ((l=stream.readLine())!=null){Output.write(l);};
Output.flush();
Output.close();
}
catch (Exception e){System.out.println("Errore di lettura.");}; /*qui cattura l'eccezione*/
Originariamente inviato da agadula
Ti allego solo il codice modificato su tua indicazione:
System.out.println(url.getFile()); /* qui stampa: /doc/ppp.htm */
File out=new File(".\\"+url.getFile());
con queste istruzioni cerca di aprire un file che si chiama .\\/doc/ppp.htm
sostituisci con
File out=new File("./file_di_prova");
se funziona poi metti qualcosa di significativo al posto della stringa "./file_di_prova"
FUNZIONA!!!!:D :D :D
GRAZIE kingv, DAVVERO TANTISSSSSSIME GRAZIE!
SENZA DI TE NON SAREI MAI RIUSCITO!!
Grazie davvero...:cool:
...non ho parole....
vBulletin® v3.6.4, Copyright ©2000-2025, Jelsoft Enterprises Ltd.