1

I want to write a Java program which will hit a URL and will print the status code(i.e., 200, 404, etc.). I am doing this using the HttpUrlConnection api, but it only shows the exception, and does not print the status code.

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);
8
  • 1
    but it only shows the exception what is that exception? Commented Aug 23, 2013 at 5:16
  • connection timed out. Commented Aug 23, 2013 at 5:16
  • 1
    Connection timed out is not an HTTP response code. There just was no response at all. Commented Aug 23, 2013 at 5:17
  • How about to use HttpURLConnection#setReadTimeout! and do proper exception handling too... Commented Aug 23, 2013 at 5:18
  • Try setting connection.setConnectTimeout(8000); like this? Commented Aug 23, 2013 at 5:18

4 Answers 4

3

I tried the your code as the following and it worked fine for me:

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

        }

    }
}


also with google.

Sign up to request clarification or add additional context in comments.

Comments

1

You will get a response code from a URL only if you are able to reach that URL. In your code you seems to be using a non existing URL and hence must be getting not reachable host exception.

Try to reach a valid URL and check the response code:

URL url = new URL("https://google.com");

Comments

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

Try using some other url, because the url you are using is invalid. 'https://abc.com/test.html' - no such page exists, so it is giving exception. try a valid url and it will work fine.

Comments

0

Even I am getting the same exception i.e java.net.ConnectException: Connection timed out: connect if I am not adding the proxy & port in your code.

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)

This exception you are getting because you are executing the code on your organization behind your organization proxy.

please use the below modified code. you will get the 200 OK as 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();
            }

        }

    }

NOTE: 1. You need to get the proxyHost IP from your organization network admin. 2. You should use HttpsURLConnection for accessing https URL.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.