URL and FileNotFoundException

Discussion in 'Java' started by Ryan Stewart, Dec 7, 2004.

  1. Ryan Stewart

    Ryan Stewart Guest

    I haven't worked much with URLs and such. The following code works almost as
    desired. The only problem is that if "blah.jsp" doesn't exist on
    http://localhost:<port>/, it throws a FileNotFoundException. If I request an
    existing resource, it returns the resource just fine (though I somewhat
    expected to get the HTTP headers as well). For a non-existing resource, I
    was looking for it to return the HTML of the server's 404 page. Essentially,
    I want this program to act as a mini-browser and always return the server's
    response, not throw exceptions like this. Should I just make a straight
    Socket connection to the server and read back the full response manually?

    import java.io.*;
    import java.net.*;

    public class URLTester {

    public static final int TEST_PORT = 8080;

    private int port;

    public URLTester(int port) {
    this.port = port;
    }

    public void work() throws MalformedURLException, IOException {
    URL url = new URL("http://localhost:" + port + "/blah.jsp");
    System.out.println("** Requesting " + url);
    InputStream in = url.openStream();
    int data;
    while ((data = in.read()) != -1) {
    System.out.print((char) data);
    }
    in.close();
    }

    public static void main(String[] args) {
    URLTester tester = new URLTester(TEST_PORT);
    try {
    tester.work();
    } catch (MalformedURLException mue) {
    mue.printStackTrace();
    } catch (IOException ioe) {
    ioe.printStackTrace();
    }
    }
    }
    Ryan Stewart, Dec 7, 2004
    #1
    1. Advertising

  2. Ryan Stewart

    Sudsy Guest

    Ryan Stewart wrote:
    <snip>
    > public void work() throws MalformedURLException, IOException {
    > URL url = new URL("http://localhost:" + port + "/blah.jsp");
    > System.out.println("** Requesting " + url);

    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    if( conn.getResponseCode() != HttpURLConnection.HTTP_OK ) {
    // process error
    return;
    }
    InputStream in = conn.getInputStream();
    > int data;
    > while ((data = in.read()) != -1) {
    > System.out.print((char) data);
    > }
    > in.close();
    > }

    <snip>

    --
    Java/J2EE/JSP/Struts/Tiles/C/UNIX consulting and remote development.
    Sudsy, Dec 7, 2004
    #2
    1. Advertising

  3. Ryan Stewart

    Ryan Stewart Guest

    "Sudsy" <> wrote in message
    news:...
    > Ryan Stewart wrote:
    > <snip>
    >> public void work() throws MalformedURLException, IOException {
    >> URL url = new URL("http://localhost:" + port + "/blah.jsp");
    >> System.out.println("** Requesting " + url);

    > HttpURLConnection conn = (HttpURLConnection)
    > url.openConnection();
    > if( conn.getResponseCode() != HttpURLConnection.HTTP_OK ) {
    > // process error
    > return;
    > }
    > InputStream in = conn.getInputStream();
    >> int data;
    >> while ((data = in.read()) != -1) {
    >> System.out.print((char) data);
    >> }
    >> in.close();
    >> }

    > <snip>
    >

    Thanks a bunch. I see how it works now. That's also handy for catching
    redirects.
    Ryan Stewart, Dec 8, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Prodip Saha
    Replies:
    7
    Views:
    867
    NitRaGs
    Apr 15, 2009
  2. Dan Sikorsky

    get_aspx_ver.aspx and FileNotFoundException

    Dan Sikorsky, Feb 27, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    1,548
    Mark Rae
    Feb 27, 2006
  3. Jared Warren
    Replies:
    3
    Views:
    401
    Andy H
    Nov 9, 2006
  4. Namit

    WebServices and FileNotFoundException

    Namit, Jan 4, 2004, in forum: ASP .Net Web Services
    Replies:
    1
    Views:
    131
    Nakul
    Jan 4, 2004
  5. Graham Allwood

    FileNotFoundException and Proxy constructor

    Graham Allwood, Apr 14, 2004, in forum: ASP .Net Web Services
    Replies:
    2
    Views:
    119
    Jan Tielens
    Apr 15, 2004
Loading...

Share This Page