Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

Getting http response code

Thread view: 
Larry Coon - 08 Jan 2006 20:29 GMT
Quick question -- If I do url processing such as:

try {
 URL url = new URI(address).toURL();
 InputStream is = url.openStream();
} catch (IOException x) {
 . . .
}

is there a way in the catch block to get the http
response code (eg: 404) if there is one?
James Westby - 08 Jan 2006 20:38 GMT
> Quick question -- If I do url processing such as:
>
[quoted text clipped - 7 lines]
> is there a way in the catch block to get the http
> response code (eg: 404) if there is one?

url.openStream() is shorthand for url.openConnection().getInputStream().

You need to get the URLConnection to get the response code. Try using
the two separate methods, like

HttpURLConnection connection = (HttpURLConnection) url.getConnection();
InputStream is = connection.getInputStream();

You can then use connection.getResponseCode() to get the response code
and use the fields of HttpURLConnection to determine which code you have.

James
James Westby - 08 Jan 2006 20:45 GMT
>> Quick question -- If I do url processing such as:
>>
[quoted text clipped - 20 lines]
>
> James

Apologies, that should be

 HttpURLConnection connection = (HttpURLConnection)url.openConnection();
 InputStream is = connection.getInputStream();

James
Larry Coon - 09 Jan 2006 01:01 GMT
> Apologies, that should be
>
>   HttpURLConnection connection = (HttpURLConnection)url.openConnection();
>   InputStream is = connection.getInputStream();

That was exactly what I needed -- thanks!

Larry Coon
University of California
Thomas Hawtin - 08 Jan 2006 20:53 GMT
> Quick question -- If I do url processing such as:
>
[quoted text clipped - 7 lines]
> is there a way in the catch block to get the http
> response code (eg: 404) if there is one?

If you use URL.openConnection, it shouldn't actually connect straight
away. Cast the URLConnection to HttpURLConnection (assuming you had an
http or https URL). When you use the connection you might get an
exception (the exact behaviour changed at some point, IIRC), but still
have access to the original connection object.

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.