我正在尝试自动查找页面的所有断开的链接。我在这里看过很多文章,但都没有帮助。我面临的真正问题是我无法得到(撤回)正确的httpresponse代码。以下是代码:
public static int getResponseCode(String urlString) {
try {
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
return connection.getResponseCode();
} catch (Exception e) {
}
return -1;
}
答案 0 :(得分:0)
使用java无法单独使用此代码获得响应。 你需要java selenium驱动程序代码才能实现。
使用以下代码获得正确的回复:
private static int statusCode;
public static void main(String... args) throws IOException{
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://www.google.com/");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
List<WebElement> links = driver.findElements(By.tagName("a"));
for(int i = 0; i < links.size(); i++){
if(!(links.get(i).getAttribute("href") == null) && !(links.get(i).getAttribute("href").equals(""))){
if(links.get(i).getAttribute("href").contains("http")){
statusCode= intgetResponseCode(links.get(i).getAttribute("href").trim());
if(statusCode == 403){
System.out.println("HTTP 403 Forbidden # " + i + " " + links.get(i).getAttribute("href"));
}
}
}
}
}
public static int getResponseCode(String urlString) throws MalformedURLException, IOException{
URL url = new URL(urlString);
HttpURLConnection huc = (HttpURLConnection)url.openConnection();
huc.setRequestMethod("GET");
huc.connect();
return huc.getResponseCode();
}
否则,您可以通过将响应方法设置为“HEAD”[如果它是一个简单的测试]来获得响应。
希望它有所帮助。干杯!答案 1 :(得分:0)
以下代码适用于给定的示例网址:
public static int getResponseCode(String urlString) throws IOException {
// String url = "http://www.google.com/search?q=mkyong";
String url = "https://www.google.co.in/intl/en/about.html?fg=1";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// optional default is GET
con.setRequestMethod("GET");
//add request header
con.setRequestProperty("User-Agent", "Mozilla/5.0");
con.connect();
int responseCode = con.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
}
我得到的输出:
Sending 'GET' request to URL : https://www.google.co.in/intl/en/about.html?fg=1
Response Code : 200