Java中请求一个URL获取到状态值
工程中用到需要请求一个URL根据返回的状态值进行判断,js用ajax请求就可以了,但是Java中并不知道怎么用,搜索了一圈找到一个相对合适的。代码如下
HttpClient client = new HttpClient();
GetMethod getMethod = new GetMethod("www.baidu.com");
int res = client.executeMethod(getMethod);
System.out.print(res);
就是这么简单暴力,其他的都是渣渣,res就是返回的状态值,成功是200,失败是404.根据res进行判断即可。
网友评论