How do i check if a webpage exists with java?

泄露秘密 提交于 2019-12-23 02:17:18

问题


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.



来源:https://stackoverflow.com/questions/13171012/how-do-i-check-if-a-webpage-exists-with-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!