4

I want to check the status code of request to url but getting code

java.net.ConnectException: connect: Address is invalid on local machine or port is not valid on remote machine

here is my code

public static boolean linkExists(String URLName){
        URLName = "http://www.google.com";
        try {
          HttpURLConnection.setFollowRedirects(false);
          HttpURLConnection con =
             (HttpURLConnection) new URL(URLName).openConnection();
          con.connect();
          System.out.println(con.getResponseCode());
          return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
        }
        catch (Exception e) {           
           return false;
        }
  }

stack trace

java.net.ConnectException: connect: Address is invalid on local machine, or port is not valid on remote machine
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:69)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:157)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)
    at java.net.Socket.connect(Socket.java:579)
    at java.net.Socket.connect(Socket.java:528)
    at sun.net.NetworkClient.doConnect(NetworkClient.java:180)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:378)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:473)
    at sun.net.www.http.HttpClient.<init>(HttpClient.java:203)
    at sun.net.www.http.HttpClient.New(HttpClient.java:290)
    at sun.net.www.http.HttpClient.New(HttpClient.java:306)
    at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:995)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:931)
    at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:849)
    at com.sls.lms.test.utilities.Utilities.linkExists(Utilities.java:70)
    at com.sls.lms.test.report.ReportTest.checkReportData(ReportTest.java:70)
    at com.sls.lms.test.report.ReportTest.CheckReportForGST(ReportTest.java:30)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:138)
    at org.testng.junit.JUnit4TestRunner.start(JUnit4TestRunner.java:81)
    at org.testng.junit.JUnit4TestRunner.run(JUnit4TestRunner.java:69)
    at org.testng.TestRunner$1.run(TestRunner.java:682)
    at org.testng.TestRunner.runWorkers(TestRunner.java:1012)
    at org.testng.TestRunner.privateRunJUnit(TestRunner.java:713)
    at org.testng.TestRunner.run(TestRunner.java:614)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
    at org.testng.SuiteRunner.run(SuiteRunner.java:240)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1198)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1123)
    at org.testng.TestNG.run(TestNG.java:1031)
    at org.testng.TestNG.privateMain(TestNG.java:1338)
    at org.testng.TestNG.main(TestNG.java:1307)
coure2011
  • 40,286
  • 83
  • 216
  • 349

9 Answers9

10

I believe that this issue is caused by Java attempting to use an IPV6 address, when either your OS doesn't support it, or is not set up properly to handle it.

You can force Java to use an IPV4 address with the following property:

-Djava.net.preferIPv4Stack=true

GreyBeardedGeek
  • 29,460
  • 2
  • 47
  • 67
  • I am using windows 8. how to force Java here? I tried same above code it works fine on windows 7 – coure2011 Feb 27 '13 at 11:40
  • 1
    the java.net.preferIPv4Stack *should* work on all platforms, however, I'm not a Windows user, so I can't test it there - sorry. – GreyBeardedGeek Feb 27 '13 at 14:51
  • found the command to set the above property on windows... see http://stackoverflow.com/questions/11850655/how-i-can-disable-ipv6-stack-use-for-ipv4-ips-on-jre – coure2011 Feb 28 '13 at 19:35
1

One thing I notice is that your URL connection has not been used... you haven't called connect(). I would be disappointed in the error message if that fixed your problem, but you probably can't get a response code until after you have made a request.

http://docs.oracle.com/javase/6/docs/api/java/net/URLConnection.html

Gus
  • 6,719
  • 6
  • 37
  • 58
0

Works fine for me with Eclipse. Definitely take Nikolay's advice and check if you have a firewall running. Any forum post I have seen related to this exception have been related somehow to this situation.

Gabriel Ryan Nahmias
  • 2,135
  • 2
  • 26
  • 39
0

Close all java programs and open up run (winkey + R) or just search run if you have a start button like me, and write -Djava.net.preferIPv4Stack=true in. :)

Boop
  • 1
0

Encountered this problem but just shutdown my antivirus and phew it is working, this has to do with the antivirus blocking the ports.

zeddarn
  • 302
  • 2
  • 14
0

i faced the same problem. i have windows8 64 use netbeans7.4. my antivirus is kaspersky. the solution is to turn of your antivirus or make the antivirus pause then run the code it will work

import java.net.*;
import java.io.*;

public class URLReader {
    public static void main(String[] args) throws Exception {

        URL oracle = new URL("http://www.oracle.com/");
        BufferedReader in = new BufferedReader(
        new InputStreamReader(oracle.openStream()));

        String inputLine;
        while ((inputLine = in.readLine()) != null)
            System.out.println(inputLine);
        in.close();
    }
} 
Kirk Woll
  • 76,112
  • 22
  • 180
  • 195
0

I have faced the same problem.My anitvirus is kaspersky.the solution for this is,open kapsperksy->settings->Anti virus protection->moinitored ports->select

priyanka
  • 11
  • 2
0

I got this message when trying to open Socket with IPv4 address and Port 0. May be you should check the port used when you see this message.

picsoung
  • 6,314
  • 1
  • 18
  • 35
0

I was creating a socket connection to a remote server by providing IP address and port number of the server. Was able to ping the server and was also able to telnet to the port. So no connectivity issues, still got same error posted in the question. I made a silly mistake of passing in an empty string to creating the socket instance. It is useful to use debug mode to point out such mistakes.

user55926
  • 315
  • 1
  • 14