This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with java.net.HttpUrlConnection
 
Similar Threads
how to connect to oracle db table through java program using unix shell script?
How do you include libraries (JMF specifically)?
url error ,please help
please help me in how to create a java application to give url and get the requested page
How to capture a response from a Server?