Posts

Showing posts from October, 2014

Java program to access an URL

Hi Everyone, in this blog post I will be writing a java program to access an URL. The Java program to connect to an URL is as follows: package com.blog.techjourney; import java.io.IOException; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; public class AccessURL { public static void main(String[] args) { String url = "http://sujithforu.blogspot.com/" ; String fakeurl = "http://thisisanincorrecttypeofurl.com/" ; System.out.println( " Connecting to existing url - " +url); getURLResponse(url); System.out.println(); System.out.println( " Connecting to fake url - " +fakeurl); getURLResponse(fakeurl); } public static void getURLResponse(String str) { URL url; URLConnection urlConnection; HttpURLConnection httpConnection = null ; try { url = new URL(str); urlConnection = url.openConnection(); urlConnection.co