/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.redditprog.webcrawler;

import java.net.HttpURLConnection;
import java.net.URL;

/**
 *
 * @author ryan
 */
public class SubRedditChecker {
    public static boolean verifySubReddit(String sub) {
        
        try {
            // set the full url of the user input subreddit
            final URL url =  new URL("http://reddit.com/r/" + sub);
            
            // create a new connection
            HttpURLConnection huc = (HttpURLConnection) url.openConnection();
            
            // disable any redirect, since reddit automatically use search if
            // subreddit does not exist
            huc.setInstanceFollowRedirects(false);
            
            // Uses HEAD as request for faster link check
            huc.setRequestMethod("HEAD");
            
            return (huc.getResponseCode() == HttpURLConnection.HTTP_OK);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        
    }
    
}

@@ -214,16 +214,44 @@
 awler;%0A%0A
+import java.io.InputStream;%0A
 import j
@@ -277,16 +277,16 @@
 ection;%0A
-
 import j
@@ -358,16 +358,17 @@
 ecker %7B%0A
+%0A
     publ
@@ -415,25 +415,17 @@
  sub) %7B%0A
-        
 %0A
+
         
@@ -517,17 +517,16 @@
 RL url =
- 
  new URL
@@ -557,28 +557,16 @@
 + sub);%0A
-            
 %0A       
@@ -675,29 +675,17 @@
 tion();%0A
-            
 %0A
+
         
@@ -798,72 +798,8 @@
 ist%0A
-            huc.setInstanceFollowRedirects(false);%0A            %0A
     
@@ -923,69 +923,489 @@
     
-return (huc.getResponseCode() == HttpURLConnection.HTTP_OK
+// connects to the http%0A            huc.connect();%0A%0A            // Possible solution (do not delete)%0A            //System.out.println(%22connected url: %22 + huc.getURL());%0A%0A            // Extract the redirect url in string%0A            InputStream is = huc.getInputStream();%0A            String redirectURL = huc.getURL().getPath();%0A            is.close();%0A            %0A            // checks if it is a redirect and return boolean value%0A            return redirectURL.contains(%22/r/%22
 );%0A
+%0A
     
@@ -1505,26 +1505,14 @@
   %7D%0A
-        
 %0A
+
     %7D%0A
-    
 %0A%7D%0A

Fix link checker bug