URL url=null;The URLConnection object is created each time the connection is made with openConnection(). Now that the connection is successfully established, you can use URLConnection to read and write to the url with the InputStream and outputStream.
try {
url = new URL("http://www.codeurjava.com");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
URLConnection con = url.openConnection();
in.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e.printStackTrace();
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
public class URLConnectionExample:{
public static void main(String[] args) {
String host = "http://www.codeurjava.com/";
URL aURL = null;
String codeHTML = "";
try {
aURL = new URL(host);
//open connection
URLConnection con = aURL.openConnection();
//maximum time allotted to connect
con.setConnectTimeout(60000);
//maximum time allotted to read
con.setReadTimeout(60000);
//read stream with UTF-8 character encoding
BufferedReader in = new BufferedReader(
new InputStreamReader(
con.getInputStream(),"UTF-8"));
String inputline;
while((inputline = in.readLine())!=null){
//concatenation+newline with \n
HTML code += inputline+"\n";
}
//the playback stream must be closed
in.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(codeHTML);
}
}
< head>
< title> JAVA Development Tutorials and Examples
.
.
.
Please disable your ad blocker and refresh the window to use this website.