Moe
Moe

Reputation: 470

Check if a string contain Error 404

What's a simple method of checking if a string contain error 404? The string was built from HttpGet.

Any help is appreciated.

Upvotes: 1

Views: 1335

Answers (2)

keyser
keyser

Reputation: 19189

How about the String method contains? No fuzz. If you want to do some more advanced search regex might be needed.

Code:

String s = "Foo error 404 bar";
System.out.println(s.contains('error 404');

prints true.

Note that this is not a good way to find errors (see Udo Klimaschewski's answer for example).

Upvotes: 1

Udo Klimaschewski
Udo Klimaschewski

Reputation: 5315

You should better check the status code, the result string of a HTTP request is not guaranteed to contain the error code, if the status is 404.

If it's Apache HTTP client (which it looks like, because you mentioned HttpGet):

get = new HttpGet("http://www.google.com");
response = client.execute(get);
if (response.getStatusLine().getStatusCode() == 404) {
    System.out.println("404 - Not found!");
}

Upvotes: 2

Related Questions