Christian
Christian

Reputation: 115

Java 404 Checker Connect In Progress

Trying to get a simple java program running. Whenever I try this code though I get a Exception in thread "main" java.lang.IllegalStateException: connect in progress

Here is the code.

public class Checker404 
{

public static int getResponseCode(String urlString) throws MalformedURLException, IOException 
{
    URL u = new URL(urlString); 
    HttpURLConnection huc =  (HttpURLConnection)  u.openConnection(); 
    huc.connect(); 

    huc.setRequestMethod("HEAD"); 
    huc.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 (.NET CLR 3.5.30729)");
    huc.disconnect();
    return 1;    
}  

public static void main(String[] args) throws Exception 
{
    final  String URL = "http://www.asda.com";
    Checker404.getResponseCode(URL);

}
}

I've tried changing around the huc.connect to before the request property and requestMethod, it will just compile but not do anything. But when it is like this I get the connect in progress error. Any advice would be welcome. I am a bit new to programming so forgive any inconsistencies I have.

Upvotes: 1

Views: 2349

Answers (2)

Joseph Hansen
Joseph Hansen

Reputation: 13319

If that is all of your code, then the correct result is that it "does nothing." It will do the Checker404 on the URL, get a result of 1 and do nothing with the result. The huc.connect() should be just before huc.disconnect().

Upvotes: 0

tsolakp
tsolakp

Reputation: 5948

huc.connect() should be just before huc.disconnect().

Upvotes: 1

Related Questions