如何在Java中获取URL的HTTP响应代码? [英] How to get HTTP response code for a URL in Java?
本文介绍了如何在Java中获取URL的HTTP响应代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请告诉我获取特定网址的响应代码的步骤或代码。
Please tell me the steps or code to get the response code of a particlular URL.
推荐答案
URL url = new URL("http://example.com");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
int code = connection.getResponseCode();
这绝不是一个有力的例子;你需要处理 IOException
s等等。但它应该让你开始。
This is by no means a robust example; you'll need to handle IOException
s and whatnot. But it should get you started.
如果你需要更多功能的东西,请查看 HttpClient 。
If you need something with more capability, check out HttpClient.
这篇关于如何在Java中获取URL的HTTP响应代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文