I'm trying to access the contents of a URL using java.net.URL class, but geting following output:
Host: java2s.com
Protocol: http
DefaultPort: 80
File: /
ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
Please help me to sort out this problem.
import java.io.InputStream;
import java.net.URL;
public class URLTest {
public static void main(String[] args) {
URL ObjUrl = null;
InputStream ObjInputStr = null;
try {
ObjUrl = new URL("http://java2s.com/");
System.out.println("Host: " + ObjUrl.getHost());
System.out.println("Protocol: " + ObjUrl.getProtocol());
System.out.println("DefaultPort: " + ObjUrl.getDefaultPort());
System.out.println("File: " + ObjUrl.getFile());
ObjInputStr = ObjUrl.openStream();
int next = -1;
while ( (next = ObjInputStr.read()) != -1) {
System.out.print((char)next);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Aucun commentaire:
Enregistrer un commentaire