How do i check if a webpage exists with java?

2019-05-31 00:57发布

问题:

So here's the deal. I don't have it yet, but starting Friday, I'm in the process of making a forum. What I'd like to do is check to see if a user is registered on the forum.

I'm not sure exactly, but lets say the users are stored in URL/users/NAME.WHATEVER (jsp?)

how, in java, do i make a URL connection, and check to see if URL/users/Robert.WHATEVER == null?

回答1:

Even you could have googled your answer: One simple way is to use the Apache Http Components

HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://localhost/users/Robert.WHATEVER");
HttpResponse response = httpclient.execute(httpget);
if (response.getStatusLine().getStatusCode() == 404) {
   System.out.println("User Robert.WHATEVER not found");
}

There is a good Tutorial with many examples which explains how to use the HttpClient.

Another highlevel framework is the ResteasyClientFramework.