Charles Bell
Posts: 519
Nickname: charles
Registered: Feb, 2002
|
|
Re: what's wrong with this piece of code?
|
Posted: Mar 10, 2002 5:39 PM
|
|
There was more to it than I remembered this morning. The server may return some html if the page does not exist. So in the case of the url you mentioned in the previous code, it will return several lines, one of which reads "The page cannot be found" This probably will not work in all cases, but if you are dealing with just one server you can at least experiment and see what it gives back for missing pages. I tried "http://www.gnu.org/" which does really exist and the program will read all the line of html returned just fine
I tried "http://www.gnu.org/qqq.html" which does not exist and I got an IOException and I caught it in the catch block to indicate that the file was not there.
From what I remember, an almost fool proof way is to try and get an HTTPURLConnection and read the response code. If you get a 200 or HTTPURLConnection.HTTP_OK then everything is okay and the file exists. You can get an HttpURLConnection with the following method.
/** Returns a HttpURL connection based upon the input url string. */ public HttpURLConnection getHttpURLConnection(String urlstring){ HttpURLConnection connection = null; try{ if (debug) System.out.println("Connecting to " + urlstring); URL url = new URL(urlstring); connection = (HttpURLConnection)url.openConnection(); }catch(MalformedURLException murle){ System.out.println("MalformedURLException: " + murle.getMessage()); if (debug) System.out.println("Returning null connection"); return null; }catch(IOException ioe){ System.out.println("IOException: " + ioe.getMessage()); return null; } if (connection == null) System.out.println("Returning null connection"); return connection; }
Anyway, this is what I have so far.
import java.awt.*; import java.io.*; import java.net.*; import java.applet.*; import java.awt.event.*;
public class Applet2 extends Applet implements ActionListener { Button button; boolean exists; URL address; Object object;
public void init(){ button = new Button("click"); add(button); button.addActionListener(this); exists = false; }
public void paint(Graphics g){ if(exists){ g.drawString("exists",10,10); } else{ g.drawString("does not exist",10,10); } }
public void actionPerformed(ActionEvent evt){ if (evt.getSource() == button){ try{ //String temp = "http://studwww.rug.ac.be/~pprovoos/pierkunde.html"; //String temp = "http://www.gnu.org/";//this does really exist String temp = "http://www.gnu.org/qqq.html";//this does not address = new URL(temp); //object = address.getContent(); String file = address.getFile(); if (file.length() > 0) { try{ URLConnection connection = address.openConnection(); String type = connection.getContentType(); //System.out.println(type); if (type.compareToIgnoreCase("text/html") == 0){ InputStream is = connection.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String lineread = ""; String html = ""; while ((lineread = br.readLine()) != null){ html = html + lineread; //System.out.println(lineread); } String cantbefound = "The page cannot be found"; if (html.toLowerCase().indexOf(cantbefound.toLowerCase()) >= 0){ exists = false; }else{ exists = true; } }
}catch(IOException ioe){ System.err.println("IOException: " + ioe.getMessage()); exists = false;
} }
} catch(Exception e){ exists = false; } repaint(); } } } [/pre}
|
|