0% found this document useful (0 votes)
31 views4 pages

DCN Computer Science Sem 5

The document discusses using URLConnection to read from a URL. It imports necessary packages and defines a main method that creates a URLConnection object for a URL, opens the connection, and reads the input stream line by line, printing it. It then closes the buffered reader. A second example demonstrates methods of URLConnection like getDate(), getContentType(), getExpiration(), getLastModified(), and getContentLength(). If content length is greater than 0, it prints the content by reading the input stream character by character.

Uploaded by

Chetan Gohil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views4 pages

DCN Computer Science Sem 5

The document discusses using URLConnection to read from a URL. It imports necessary packages and defines a main method that creates a URLConnection object for a URL, opens the connection, and reads the input stream line by line, printing it. It then closes the buffered reader. A second example demonstrates methods of URLConnection like getDate(), getContentType(), getExpiration(), getLastModified(), and getContentLength(). If content length is greater than 0, it prints the content by reading the input stream character by character.

Uploaded by

Chetan Gohil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Name:NIDHI.B.

ROLLNO:3504

Category 2

Aim:Study of URLConnection and read from URLConnection...

import java.net.*;

import java.io.*;

public class URLConnectionReader

public static void main(String[]args)throws Exception

URL oracle =new URL("http://www.flipkart.com/");

URLConnection yc=oracle.openConnection();

BufferedReader in=new BufferedReader(new InputStreamReader(yc.getInputStream()));

String inputLine;

while((inputLine=in.readLine())!=null)

System.out.println(inputLine);

in.close();

}
Name:NIDHI.B.ROLLNO:3504
Name:NIDHI.B.ROLLNO:3504

Aim:Demostrating methods of URLCONNECTION.

import java.net.*;

import java.io.*;

import java.util.Date;

class UCDemo{

public static void main(String args[])throws Exception{

int c;

URL hp=new URL("http://www.google.com/");

URLConnection hpCon=hp.openConnection();

System.out.println("Date:"+new Date(hpCon.getDate()));

System.out.println("Content-Type:"+hpCon.getContentType());

System.out.println("Expires:"+hpCon.getExpiration());

System.out.println("Last-Modified:"+new Date(hpCon.getLastModified()));

int len=hpCon.getContentLength();

System.out.println("Content Length:"+len);

if(len>0)

System.out.println("===Content===");

InputStream input=hpCon.getInputStream();

int i=len;

while((c=input.read())!=-1)

System.out.print((char)c);

input.close();
Name:NIDHI.B.ROLLNO:3504

else

System.out.println("No Content available");

You might also like