如何检查java是否存在网页? [英] How do i check if a webpage exists with java?
问题描述
所以这是交易。我还没有,但从星期五开始,我正在建立一个论坛。我想做的是检查用户是否在论坛上注册。
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.
我不确定,但可以说用户存储在URL / users / NAME.WHATEVER(jsp?)
I'm not sure exactly, but lets say the users are stored in URL/users/NAME.WHATEVER (jsp?)
如何在java中建立URL连接,并检查URL / users / Robert.WHATEVER == null?
how, in java, do i make a URL connection, and check to see if URL/users/Robert.WHATEVER == null?
推荐答案
即使你可以搜索你的答案:
一种简单的方法是使用 Apache Http组件
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");
}
有一个很好的教程,其中包含许多解释如何使用HttpClient的示例。
There is a good Tutorial with many examples which explains how to use the HttpClient.
另一个高级框架是 ResteasyClientFramework 。
Another highlevel framework is the ResteasyClientFramework.
这篇关于如何检查java是否存在网页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!