Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > URL and FileNotFoundException

Reply
Thread Tools

URL and FileNotFoundException

 
 
Ryan Stewart
Guest
Posts: n/a
 
      12-07-2004
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();
}
}
}


 
Reply With Quote
 
 
 
 
Sudsy
Guest
Posts: n/a
 
      12-07-2004
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.

 
Reply With Quote
 
 
 
 
Ryan Stewart
Guest
Posts: n/a
 
      12-08-2004
"Sudsy" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
GAC and FileNotFoundException Can someone help? Prodip Saha ASP .Net 7 04-15-2009 02:18 PM
AzAuthorizationStore.Initialize on ADAM Url throws FileNotFoundException Jared Warren ASP .Net Security 3 11-09-2006 05:49 PM
get_aspx_ver.aspx and FileNotFoundException Dan Sikorsky ASP .Net 4 02-27-2006 08:33 PM
FileNotFoundException and Proxy constructor Graham Allwood ASP .Net Web Services 2 04-15-2004 05:25 AM
WebServices and FileNotFoundException Namit ASP .Net Web Services 1 01-04-2004 10:22 AM



Advertisments