2

I am developing a sanity check web application. I tried getting url response using HttpUrlConnection method but I am getting UnknownHostException.

 System.setProperty("java.net.preferIPv4Stack" , "true");
    String[] uat_targetUrls={"https://www.google.com"};
    String[] uat_targetResponse=new String[uat_targetUrls.length];

            HttpURLConnection httpUrlConn;
            httpUrlConn = (HttpURLConnection) new URL(uat_targetUrls[i])
            .openConnection();

            httpUrlConn.setRequestMethod("GET");


            httpUrlConn.setConnectTimeout(30000);
            httpUrlConn.setReadTimeout(30000);



           if(httpUrlConn.getResponseCode()==200)
               uat_targetResponse[i]="UP";
           else 
               uat_targetResponse[i]="DOWN";

When executing this, I am getting UnknownHostException for various urls. Can anyone help me on this. I am using Eclipse IDE. This is the error I am getting:

java.net.UnknownHostException: www.google.com
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)

Thanks.

4
  • Maybe you need to configure a proxy? Commented May 23, 2014 at 9:31
  • I am able to access the url through browser but not getting reply from host on ping. Commented May 23, 2014 at 9:38
  • What does Preferences > General > Network Connections in Eclipse look like? Commented May 23, 2014 at 9:45
  • I can see two sections : Proxy Entries and Proxy ByPass. There are 2 entries in Proxy ByPass->localhost and 127.0.0.1. In Proxy entries HTTP is checked. Commented May 23, 2014 at 9:58

4 Answers 4

2

The problem must be a networking issue on your machine.

Your code works for me (with some minor fixes to repair the missing loop variable i):

public static void main(String[] args) throws Exception {
    System.setProperty("java.net.preferIPv4Stack", "true");
    String[] uat_targetUrls = { "https://www.google.com" };
    String[] uat_targetResponse = new String[uat_targetUrls.length];

    HttpURLConnection httpUrlConn;
    httpUrlConn = (HttpURLConnection) new URL(uat_targetUrls[0])
            .openConnection();

    httpUrlConn.setRequestMethod("GET");

    httpUrlConn.setConnectTimeout(30000);
    httpUrlConn.setReadTimeout(30000);

    if (httpUrlConn.getResponseCode() == 200)
        uat_targetResponse[0] = "UP";
    else
        uat_targetResponse[0] = "DOWN";


    System.out.println(uat_targetResponse[0]);
}

Output: UP

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

2 Comments

Yeah that works for me too on my local machine. I am facing problem when using it from my organization's desktop. Do you have a fix for this.
@NikhileshTiwari This is the wrong site for help fixing your network issues, I'm afraid.
1

try pinging www.google.com using

ping www.google.com -t 

if you get a time out error
Reason 1: No internet connection
Reason 2: You are probably behind an proxy server.
Reason 3: Add credentials to header

Comments

0

I tested the code provided by you, It seems working fine. UnknownHostException is thrown when IP address is not resolved. If you are in some organization, check if the network allows you to connect to network through code, or if DNS settings are proper.

3 Comments

I can access the url through my browser but when I am trying to ping the hostname it does not reply back.
can you check stackoverflow.com/questions/13670692/… If this helps?
Thanks Code Fighter, I tried changing my program using the solution given but it did not work.
0

Use ping www.google.com -4 to see if you can visit google.com by ipv4.

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.