代码很简单,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);
	}
}

相关文章:

  • 2021-07-19
  • 2021-07-25
  • 2021-08-14
  • 2021-07-01
  • 2022-12-23
  • 2022-12-23
  • 2021-06-23
  • 2021-10-27
猜你喜欢
  • 2021-10-28
  • 2021-10-13
  • 2022-01-13
  • 2021-11-20
  • 2021-09-15
  • 2021-05-31
相关资源
相似解决方案