title: Read redirect file from web server (.txt file) and verify response from page ("MalformedURLException: no protocol")
body: <p>First time posting, but have used this site more times than I can recall!  I'm very new to programming and I've been tasked to write automated test scripts with <code>Selenium WebDriver</code>, which has ultimately lead me to learning some basic (to some) Java.</p>
  
  <p>What I'm attempting to do is read a text file(a large file of redirects) that resides on a web server, loop through each line, and finally check the response from the page (which I haven't really coded yet).  I have created a script that loads each page and asserts it's not a <code>404</code>, but I'm looking for a solution that will be MUCH faster by <em>hopefully</em> just testing the http response.</p>
  
  <p>The lines in the text file may appear in the following formats:</p>
  
  <p><strong>myredirect <a href="http://www.example.com/apage.html" rel="nofollow">http://www.example.com/apage.html</a></strong> //a url external to the host</p>
  
  <p><strong>anotherredirect /apage.html</strong> //single space between the redirect and the destination path, no protocol/host</p>
  
  <p><strong>andanotherredirect  /apage.html</strong> //two spaces between the redirect and the destination path, again no protocol/host</p>
  
  <p>I'm getting the following failure:</p>
  
  <blockquote>
    <p>java.net.MalformedURLException: no protocol:</p>
  </blockquote>
  
  <p>I appreciate any assistance!</p>
  
  <pre><code>public class RedirectTestFireFox {
  WebDriver driver;
  StringBuffer verificationErrors = new StringBuffer();
  
  
  @Before
  public void setUp() throws Exception {
  
      driver = new FirefoxDriver();
      driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }
  
  @Test
  public void testRedirectTestFireFox() throws InterruptedException, IOException {
  
      //read redirects from file on web server
      URL test = new URL("https://www.example.com/redirects.txt");
  
      //get the protocol (http or https)
      String protocol = test.getProtocol();
  
      //get the host of the supplied url
      String host = test.getHost();
  
      //create baseURL variable that appends protocol "://" and the host
      String baseURL= protocol + "://" + host;
  
      //do something to each URL in text file
      try (Scanner scan = new Scanner(test.openStream())){
          //while there is still another line in the redirects file...
          while(scan.hasNextLine()) {
  
              //assign value to "line" variable
              String line = scan.nextLine();
  
              if(line != null){
  
                  String redirect = line.substring(0, line.indexOf(" "));
  
                  //find the last instance of a space and then take what is to the right of that and assign it back to line variable
                  line = line.substring(line.lastIndexOf(" ")+1, line.length());
  
                  //trim off any blank spaces from either end
                  line.trim();
  
                  //test to see if the first character starts with "/"
                  boolean firstChar = line.startsWith("/");
  
                  //if firstChar is "/" append the baseURL variable to the redirect
                  if(firstChar) {
                      line = baseURL + line;
                  }
  
                  URL u = new URL(line); //this is the where it fails!!!
                  HttpURLConnection urlConnection =(HttpURLConnection) u.openConnection (); 
                  urlConnection.setRequestMethod("GET");
                  urlConnection.connect();
                  try {
                      int code = urlConnection.getResponseCode();
                      assertEquals(200, code);
                  } catch (AssertionError e) {
                      System.err.println("Error found! " + redirect);
                  }
  
              }
  
          }
      }
  
      }
  
  @After
      public void tearDown() throws Exception {
      driver.close();
  }
  
  }
  </code></pre>