JAVA应用apache httpclient探测http服务
代码很简单,apache都已经提供了封装。
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.log4j.Logger;
public class CheckHttp {
private static final Logger LOGGER = Logger.getLogger(CheckHttp.class);
public static int testHttp(String url, int getOrPost) {
HttpClient client = new HttpClient();
HttpMethod method = getOrPost == 1 ? new GetMethod(url)
: new PostMethod(url);
try {
client.executeMethod(method);
return method.getStatusLine().getStatusCode();
} catch (Exception e) {
LOGGER.error("", e);
}
return 404;
}
public static void main(String[] args) {
String url = "http://www.facebook.com";
int i = testHttp(url, 1);
System.out.println(i);
}
}