File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Problem with java.net.HttpUrlConnection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Problem with java.net.HttpUrlConnection" Watch "Problem with java.net.HttpUrlConnection" New topic
Author

Problem with java.net.HttpUrlConnection

Sridhar Gudipalli
Ranch Hand

Joined: Nov 02, 2005
Posts: 120
Hi I have written a java program which tests the internet connectivity.

Case 1:
----------
1) Connect the network
2) Run the program
3) Disconnect the network
4) Connect the network.

In this case program is working fine.. and giving the below output.

0 InternetConnectivity is: UP
1 InternetConnectivity is: UP
2 InternetConnectivity is: UP
3 IOException occured InternetConnectivity is: DOWN
4 IOException occured InternetConnectivity is: DOWN
5 IOException occured InternetConnectivity is: DOWN
6 IOException occured InternetConnectivity is: DOWN
7 IOException occured InternetConnectivity is: DOWN
8 InternetConnectivity is: UP
9 InternetConnectivity is: UP
10 InternetConnectivity is: UP
11 InternetConnectivity is: UP

Case 2:
----------
1) Disconnect the network
2) Run the program
3) Connect the network.


In this case program works initially, but when I reconnect network cable, it is not recognizing. It is giving the below output.

0 IOException occured InternetConnectivity is: DOWN
1 IOException occured InternetConnectivity is: DOWN
2 IOException occured InternetConnectivity is: DOWN
3 IOException occured InternetConnectivity is: DOWN
4 IOException occured InternetConnectivity is: DOWN
5 IOException occured InternetConnectivity is: DOWN
6 IOException occured InternetConnectivity is: DOWN
7 IOException occured InternetConnectivity is: DOWN
8 IOException occured InternetConnectivity is: DOWN
9 IOException occured InternetConnectivity is: DOWN
10 IOException occured InternetConnectivity is: DOWN
11 IOException occured InternetConnectivity is: DOWN

I have spent time to analyze and java.net api and googled, but no use.
I really appreciate if you could guide me right direction.

Here is the program:

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;


public class NetworkTest {

public static void main(String[] args) {
for(int i=0; i<100; i++){
try {
Thread.sleep(10000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
System.out.print(i+" ");
new NetworkTest().isInternetConnectivityUp();
}

}

/**
* Tests if the internet connectivity is up
* @return true if the connectivity is up false, otherwise.
*/
public boolean isInternetConnectivityUp(){
boolean boolValue=false;
java.net.URL testUrl=null;
java.net.HttpURLConnection testConnection=null;

try {
String aUri="http://www.google.com";

URI uri = new URI(aUri);
testUrl = uri.toURL();

testConnection = (HttpURLConnection) testUrl.openConnection();

if(testConnection!=null){
testConnection.setDefaultUseCaches(false);
testConnection.connect();
int respCode = testConnection.getResponseCode();
boolValue=true;
}
}
catch (MalformedURLException e) {
System.out.print("MalformedURLException occured ");
}
catch (IOException e) {
System.out.print("IOException occured ");
}
catch (URISyntaxException e) {
System.out.print("URISyntaxException occured ");
}finally{

if(testConnection!=null){
testConnection.disconnect();
testConnection=null;
}
testUrl=null;
}

System.out.println("InternetConnectivity is: "+(boolValue?"UP":"DOWN"));
return boolValue;
}
}



Sridhar Gudipalli|SCJP 6.0
SCWCD objectives
Sridhar Gudipalli
Ranch Hand

Joined: Nov 02, 2005
Posts: 120
Finally I got the solution for this issue.

DNS name is not resolving in this case with java 1.6.0_07 version. (It should work with 1.6.0_14, not sure with other versions)
Instead URL should use IP address like..
URL url = new URL("http://xx.xx.xx.xxx"); //instead of new URL("http://somesite.com");

Thanks if anyone tried to solve this issue.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Problem with java.net.HttpUrlConnection