java tutorial - Java URLConnection class - java programming - learn java - java basics - java for beginners

  • The Java URLConnection class represents a communication link between the URL and the application.
  • This class can be used to read and write data to the specified resource referred by the URL.

How to get the object of URLConnection class

  • The openConnection() method of URL class returns the object of URLConnection class.


public URLConnection openConnection()throws IOException{}  
click below button to copy the code. By - java tutorial - team

Displaying source code of a webpage by URLConnecton class

  • The URLConnection class provides many methods, we can display all the data of a webpage by using the getInputStream() method.
  • The getInputStream() method returns all the data of the specified URL in the stream that can be read and displayed.

Sample Code

public class URLConnectionExample {  
public static void main(String[] args){  
URL url=new URL("");  
URLConnection urlcon=url.openConnection();  
InputStream stream=urlcon.getInputStream();  
int i;  
}catch(Exception e){System.out.println(e);}  
click below button to copy the code. By - java tutorial - team

Related Searches to Java URLConnection class