2013-08-23 127 views
1

我想编写一个Java程序,它会打一个URL并打印状态码(即200,404等)。我正在使用HttpUrlConnection api执行此操作,但它只显示异常,并不显示状态码。从网址获取http响应代码

URL url = new URL("https://abc.com/test.html"); 
HttpURLConnection connection = (HttpURLConnection)url.openConnection(); 
connection.setRequestMethod("GET"); 
connection.connect(); 

int code = connection.getResponseCode(); 
System.out.println("code: "+code); 
+1

_但它只显示异常_什么是异常? –

+0

连接超时。 – anonymous

+1

连接超时不是HTTP响应代码。根本没有任何回应。 – Thilo

回答

3

我试过你的代码如下所示,它为我工作得很好:

import java.net.*; 

public class Action 
{ 

    public static void main(String[] args) 
    { 
     try 
     { 
      URL url = new URL("http://localhost:8888"); 
      HttpURLConnection connection = (HttpURLConnection)url.openConnection(); 
      connection.setRequestMethod("GET"); 
      connection.connect(); 

      int code = connection.getResponseCode(); 
      System.out.println("code: "+code); 
     } 
     catch(Exception e) 
     { 

     } 

    } 
} 


还与谷歌。

1

只有当您能够到达该URL时,您才会收到URL的响应代码。在你的代码中,你似乎正在使用一个不存在的URL,因此必须无法访问主机例外。

试图达成一个有效的URL,并检查响应代码:

URL url = new URL("https://google.com"); 
0
URL url = new URL("https://www.google.com"); 
HttpURLConnection connection = (HttpURLConnection)url.openConnection(); 
connection.setRequestMethod("GET"); 
connection.connect(); 

int code = connection.getResponseCode(); 
System.out.println("code: "+code); 

请尝试使用其他网址,因为您使用的网址无效。 'https://abc.com/test.html' - 不存在这样的页面,所以它给出异常。尝试一个有效的网址,它会正常工作。

0

即使我得到相同的异常,即java.net.ConnectException:连接超时:连接,如果我不在代码中添加代理端口&。

java.net.ConnectException: Connection timed out: connect 
    at java.net.PlainSocketImpl.socketConnect(Native Method) 
    at java.net.PlainSocketImpl.doConnect(Unknown Source) 
    at java.net.PlainSocketImpl.connectToAddress(Unknown Source) 
    at java.net.PlainSocketImpl.connect(Unknown Source) 
    at java.net.SocksSocketImpl.connect(Unknown Source) 
    at java.net.Socket.connect(Unknown Source) 
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.connect(Unknown Source) 
    at com.sun.net.ssl.internal.ssl.BaseSSLSocketImpl.connect(Unknown Source) 
    at sun.net.NetworkClient.doConnect(Unknown Source) 
    at sun.net.www.http.HttpClient.openServer(Unknown Source) 
    at sun.net.www.http.HttpClient.openServer(Unknown Source) 
    at sun.net.www.protocol.https.HttpsClient.<init>(Unknown Source) 
    at sun.net.www.protocol.https.HttpsClient.New(Unknown Source) 
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(Unknown Source) 
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source) 
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source) 
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(Unknown Source) 
    at edu.sandip.TestURLConnection.main(TestURLConnection.java:23) 

由于您在组织代理后面的组织中执行代码,因此会发生此异常。

请使用下面的修改代码。你会得到200 OK作为responseCode。

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

    import javax.net.ssl.HttpsURLConnection; 



    public class TestURLConnection { 

     /** 
     * @param args 
     */ 
     public static void main(String[] args) { 
      try{ 
       URL url = new URL("https://www.google.com/"); 
       System.setProperty("https.proxyHost", "XXX.XXX.XXX.XX"); 
       System.setProperty("https.proxyPort", "80"); 
       HttpURLConnection connection = (HttpURLConnection)url.openConnection(); 
       //HttpsURLConnection connection = (HttpsURLConnection)url.openConnection(); 
       connection.setRequestMethod("GET"); 
       connection.connect(); 

       int code = connection.getResponseCode(); 
       System.out.println("code: "+code); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 

     } 

    } 

注: 1.你需要从组织的网络管理员获得的ProxyHost IP。 2.您应该使用HttpsURLConnection访问https URL。