4

我想检查对 url 的请求的状态代码,但正在获取代码

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

这是我的代码

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;
        }
  }

堆栈跟踪

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)
4

9 回答 9

10

我相信这个问题是由 Java 尝试使用 IPV6 地址引起的,而您的操作系统不支持它,或者没有正确设置来处理它。

您可以强制 Java 使用具有以下属性的 IPV4 地址:

-Djava.net.preferIPv4Stack=true

于 2013-02-27T06:43:09.910 回答
1

我注意到的一件事是您的 URL 连接尚未使用……您还没有调用 connect()。如果这解决了您的问题,我会对错误消息感到失望,但您可能在提出请求之前无法获得响应代码。

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

于 2013-02-27T06:27:29.210 回答
0

使用Eclipse对我来说效果很好。绝对听从 Nikolay 的建议,检查您是否有防火墙在运行。我看到的任何与此异常相关的论坛帖子都与这种情况有关。

于 2013-02-27T06:20:21.223 回答
0

关闭所有java程序并打开运行(winkey+ R)或者如果你有像我这样的开始按钮就搜索运行,然后写-Djava.net.preferIPv4Stack=true进去。:)

于 2013-04-22T21:35:00.163 回答
0

遇到了这个问题,但只是关闭了我的防病毒软件并发现它正在工作,这与防病毒软件阻止了端口有关。

于 2014-02-12T15:30:01.203 回答
0

我面临同样的问题。我有 windows8 64 使用 netbeans7.4。我的杀毒软件是卡巴斯基。解决方案是关闭您的防病毒软件或使防病毒软件暂停,然后运行它将起作用的代码

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();
    }
} 
于 2014-07-25T20:39:48.657 回答
0

我遇到了同样的问题。我的杀毒软件是卡巴斯基。解决方法是,打开卡巴斯基->设置->防病毒保护->监控端口->选择

于 2014-09-23T11:58:46.537 回答
0

我在尝试使用 IPv4 地址和端口 0 打开 Socket 时收到此消息。您可能应该在看到此消息时检查使用的端口。

于 2018-04-26T15:30:46.003 回答
0

我通过提供服务器的 IP 地址和端口号来创建到远程服务器的套接字连接。能够 ping 服务器,也能够 telnet 到端口。所以没有连接问题,仍然在问题中发布了相同的错误。我犯了一个愚蠢的错误,即传入一个空字符串来创建套接字实例。使用调试模式指出此类错误很有用。

于 2018-10-07T04:50:00.070 回答