我在localhost上使用WAMP服务器,我有一个小型Java软件,可以从PHP文件中获取响应。当我尝试从HttpURLConnection对象获取InputStream时(当我尝试获取responseCode或responseMessage时),我得到以下错误。请帮帮我。
错误讯息:
Exception in thread "main" java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at java.net.Socket.connect(Socket.java:538)
at sun.net.NetworkClient.doConnect(NetworkClient.java:180)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:432)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:527)
at sun.net.www.http.HttpClient.<init>(HttpClient.java:211)
at sun.net.www.http.HttpClient.New(HttpClient.java:308)
at sun.net.www.http.HttpClient.New(HttpClient.java:326)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1202)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1138)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1032)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:966)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1546)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
at javaapplication1.JavaApplication1.main(JavaApplication1.java:28)
C:\Users\Amine\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
Java代码:
public static void main(String[] args) throws MalformedURLException, IOException
{
URL url = new URL("http://localhost/test/test.php");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
System.out.println(con.getResponseCode());
}
PHP脚本(C:\ wamp \ www \ test \ test.php):
<?php echo "Hello world"; ?>
答案 0 :(得分:0)
检查防火墙设置。
看起来像是对localhost的连接:允许浏览器使用80,但是对于你的java应用程序禁用。
答案 1 :(得分:0)
好吧,我已经解决了问题,它是在wamp服务器上。 我删除它然后我再次安装它。它现在工作正常。