2017-02-28 88 views
0

我正在尝试自动化查找页面的所有断开链接。我在这里经历了很多文章,但都没有帮助。我面临的真正问题是我无法得到(回应)正确的httpresponse代码。下面是代码:在java中的Selenium WebDriver中没有得到正确的HTTP响应代码

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

什么是你所观察到的OUTPUT(HTTP响应代码)?你期望什么?你观察到了什么?请分享细节。 –

+0

即使有效的链接,我也只得到-1。对于有效的链接,我期望2XX。 –

+0

在** catch block **中,输出例外情况为'return connection.getResponseCode();'没有执行,我怀疑会抛出异常。与我们分享堆栈跟踪。 –

回答

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” [如果一个简单的测试]响应。

希望它有帮助。干杯!

+0

请提供intgetResponseCode(url)函数的代码。主要问题是函数没有返回正确的响应代码。你提供的是在得到响应代码之前和之后提供的。 –

+0

它与您使用的代码相同。你使用的代码很好。试着让我知道 –

+0

我累了,但是statusCode总是从getResponseCode()得到-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 
+0

您得到的输出结果与我期望的完全一致,但出现错误 sun.security.provider.certpath.SunCertPathBuilderException:找不到要求的目标的有效证书路径 –

+0

将SSL证书添加到java证书存储区,错误应该消失..或自动接受任何证书:O – metar

+0

@metar:我正在尝试做同样的事情。你能否向我解释过程/步骤。 –

相关问题