title: Receiving incorrect HTTP response status codes
body: <p>I'm trying to find broken links on a page.</p>
  
  <p>I fetch all the available links on the page, then loop through them.  However, sometimes, the incorrect response code is received.</p>
  
  <pre><code>List&lt;WebElement&gt; links = driver.findElements(By.tagName("a"));
  System.out.println("Total no. of links are: "+ links.size());
  java.util.Iterator&lt;WebElement&gt; it = links.iterator();
  
  while(it.hasNext()){
      WebElement element = it.next();
      url = element.getAttribute("href");
      urlname=element.getText();
  
      if(url == null || url.isEmpty()){
          System.out.println(urlname +" - "+url+ ": URL is either not configured for anchor tag or it is empty");
          continue;
      }
  
      try {
          huc = (HttpURLConnection)(new URL(url).openConnection());                       
          huc.setRequestMethod("HEAD");                       
          huc.connect();                      
          respCode = huc.getResponseCode();
  
          if(respCode &gt;= 400){
              System.out.println(urlname +" - "+ url +": is a broken link");
          }
          else{
             // System.out.println(url+" is a valid link");
          }
  
      } catch (MalformedURLException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  }
  </code></pre>