user801154
user801154

Reputation:

Getting HTTP response code in Java

I need to find the HTTP response code of URLs in java. I know this can be done using URL & HTTPURLConnection API and have gone through previous questions like this and this.

I need to do this on around 2000 links so speed is the most required attribute and among those I already have crawled 150-250 pages using crawler4j and don't know a way to get code from this library (due to which I will have to make connection on those links again with another library to find the response code).

Upvotes: 0

Views: 954

Answers (2)

kutschkem
kutschkem

Reputation: 8163

In Crawler4J, the class WebCrawler has a method handlePageStatusCode, which is exactly what you are looking for and what you would also have found if you had looked for it. Override it and be happy.

Upvotes: 3

Robert
Robert

Reputation: 42595

The answer behind your first link contains everything you need: How to get HTTP response code for a URL in Java?

    URL url = new URL("http://google.com");
    HttpURLConnection connection = (HttpURLConnection)url.openConnection();
    connection.setRequestMethod("GET");
    connection.connect();

    int code = connection.getResponseCode();

The response code is the HTTP code returned by the server.

Upvotes: 2

Related Questions