iMpRoBaBiL3
30-05-2006, 10:55
Ciao a tutti, vi espongo velocemente il mio problema: sto creando una piccola applicazione che si dovrebbe connettere ad un sito, autenticarsi e scaricare un file xml. Supponendo che il file si trovi all'indirizzo http://www.nomesito.com/xml/nome_file.php (anche se ha estensione .php è di fatto un xml... non vorrei che fosse questo il problema)
Ho creato un metodo per settarmi le variabili che utilizzerò per la connessione
protected void populateURLValues(String textURL) throws IOException {
URL url = new URL(textURL);
uri = url;
host = url.getHost ();
port = url.getPort ();
if (port == -1)
port = 80;
file = url.getFile();
}
Avevo pensato di creare una classe che estende l'Authenticator di java
public class MyAuthenticator extends Authenticator {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("mioNome", "miaPassword".toCharArray() );
}
}
Attualmente il mio metodo di connessione è fatto così:
protected Socket connect () throws IOException {
System.err.println ("Connessione a " + host + ":" + port + "...");
Authenticator.setDefault(new MyAuthenticator()); //Possibile errore
Socket socket = new Socket(host, port);
System.err.println ("Connessione avvenuta.");
BufferedOutputStream buffOut = new BufferedOutputStream (socket.getOutputStream ());
out = new DataOutputStream (buffOut);
in = new DataInputStream (socket.getInputStream ());
return socket;
}
mentre il metodo per leggere il file (per quel che serve mi basta leggerlo)
protected void getFile() throws IOException {
System.err.println ("Richiesta del file " + file + " inviata...");
out.writeBytes ("GET " + file + " HTML/1.0\r\n\r\n"); //Probabile errore
out.flush ();
System.err.println ("Ricezione dati...");
String input ;
while ((input = in.readLine ()) != null)
System.out.println (input);
}
Ma quello che ricevo quando cerco di leggere il file è la seguente risposta:
HTTP/1.1 404 Not Found
Date: XXXX
Server: Apache/2.0.51 (Fedora)
Content-Length: 304
Connection: close
Content-Type: text/html; charset=iso-8859-1
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /xml/nome_file.php was not found on this server.</p>
<hr />
<address>Apache/2.0.51 (Fedora) Server at xxxxxxxxxxxx Port 80</address>
</body></html>
Mi dite cosa sbaglio ad impostare e come dovrei farlo correttamente? Ho messo due possibili/probabili cause di errore che penso di aver individuato, ma non so come eventualmente risolverle.
Riprendetemi pure se ho scritto niubbiate ^_^
Ho creato un metodo per settarmi le variabili che utilizzerò per la connessione
protected void populateURLValues(String textURL) throws IOException {
URL url = new URL(textURL);
uri = url;
host = url.getHost ();
port = url.getPort ();
if (port == -1)
port = 80;
file = url.getFile();
}
Avevo pensato di creare una classe che estende l'Authenticator di java
public class MyAuthenticator extends Authenticator {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("mioNome", "miaPassword".toCharArray() );
}
}
Attualmente il mio metodo di connessione è fatto così:
protected Socket connect () throws IOException {
System.err.println ("Connessione a " + host + ":" + port + "...");
Authenticator.setDefault(new MyAuthenticator()); //Possibile errore
Socket socket = new Socket(host, port);
System.err.println ("Connessione avvenuta.");
BufferedOutputStream buffOut = new BufferedOutputStream (socket.getOutputStream ());
out = new DataOutputStream (buffOut);
in = new DataInputStream (socket.getInputStream ());
return socket;
}
mentre il metodo per leggere il file (per quel che serve mi basta leggerlo)
protected void getFile() throws IOException {
System.err.println ("Richiesta del file " + file + " inviata...");
out.writeBytes ("GET " + file + " HTML/1.0\r\n\r\n"); //Probabile errore
out.flush ();
System.err.println ("Ricezione dati...");
String input ;
while ((input = in.readLine ()) != null)
System.out.println (input);
}
Ma quello che ricevo quando cerco di leggere il file è la seguente risposta:
HTTP/1.1 404 Not Found
Date: XXXX
Server: Apache/2.0.51 (Fedora)
Content-Length: 304
Connection: close
Content-Type: text/html; charset=iso-8859-1
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /xml/nome_file.php was not found on this server.</p>
<hr />
<address>Apache/2.0.51 (Fedora) Server at xxxxxxxxxxxx Port 80</address>
</body></html>
Mi dite cosa sbaglio ad impostare e come dovrei farlo correttamente? Ho messo due possibili/probabili cause di errore che penso di aver individuato, ma non so come eventualmente risolverle.
Riprendetemi pure se ho scritto niubbiate ^_^