StackStalk
  • Home
  • Java
    • Java Collection
    • Spring Boot Collection
  • Python
    • Python Collection
  • C++
    • C++ Collection
    • Progamming Problems
    • Algorithms
    • Data Structures
    • Design Patterns
  • General
    • Tips and Tricks

Sunday, August 24, 2014

Java URL and URLConnection

 August 24, 2014     Java     No comments   

Java Networking APIs provide a set of classes to interact with the internet. The primary classes include URL, URLConnection and HttpURLConnection. This tutorial provides examples to construct URLs and to access internet resources.

Read properties of URL

URL represents the Uniform Resource Locator a resource on the world wide web.

URL url1 = new URL("http", "www.stackstalk.com", 
              8080, "/p/java-tutorials.html");
System.out.println(url1.toString());
This program produces the following output printing the URL as string.
http://www.stackstalk.com:8080/p/java-tutorials.html
Class URL provides functions to retrieve the properties of an URL object including protocol, host, port, path etc. Following program demonstrates reading the properties of an URL.
public static void main(String[] args) throws Exception {
  URL url = new URL("http://www.stackstalk.com/p/java-tutorials.html");
  System.out.println(url.getHost());
  System.out.println(url.getPort());
  System.out.println(url.getProtocol());
  System.out.println(url.getPath());
}
This program produces the following output printing the URL properties.
www.stackstalk.com
-1
http
/p/java-tutorials.html

Using URLConnection

URLConnection is used for accessing remote resources. We can examine the attributes of a remote resource specified as an URL and also get the contents locally.

public static void main(String[] args) throws Exception {
  URL url = new URL("http://www.stackstalk.com/p/java-tutorials.html");
  URLConnection connection = url.openConnection();
  BufferedReader reader = new BufferedReader
     (new InputStreamReader(connection.getInputStream()));
  String line = null;
  while ( ( line = reader.readLine() ) != null ) {
    System.out.println(line);
  }
  reader.close();  
}

Using HttpURLConnection

HttpURLConnection is a sub-class of URLConnection class and includes support for handling HTTP connections. HttpURLConnection class has additional methods like HTTP request method, HTTP response code, HTTP redirect handling etc.

In the example below we make a HttpURLConnection to get a page contents. Please note that additional methods specific to HTTP can be used on the connection object.

public static void main(String[] args) throws Exception {
  URL url = new URL("http://www.stackstalk.com/p/java-tutorials.html");
  HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
  System.out.println(httpConnection.getRequestMethod());
  System.out.println(httpConnection.getResponseCode());
  System.out.println(httpConnection.getResponseMessage());
  BufferedReader reader1 = new BufferedReader
     (new InputStreamReader(httpConnection.getInputStream()));
  String line1 = null;
  while ( ( line1 = reader1.readLine() ) != null ) {
    System.out.println(line1);
  }
  reader1.close();
  httpConnection.disconnect();
}

This program produces the following output displaying the HTTP request type (GET), HTTP response code (200) and HTTP response message (OK). Finally it prints the page contents.

GET
200
OK
Page contents ...
Continue to read other java tutorials from here.
  • Share This:  
Newer Post Older Post Home

0 comments:

Post a Comment

Follow @StackStalk
Get new posts by email:
Powered by follow.it

Popular Posts

  • Avro Producer and Consumer with Python using Confluent Kafka
    In this article, we will understand Avro a popular data serialization format in streaming data applications and develop a simple Avro Produc...
  • Monitor Spring Boot App with Micrometer and Prometheus
    Modern distributed applications typically have multiple microservices working together. Ability to monitor and manage aspects like health, m...
  • Server-Sent Events with Spring WebFlux
    In this article we will review the concepts of server-sent events and work on an example using WebFlux. Before getting into this article it ...
  • Implement caching in a Spring Boot microservice using Redis
    In this article we will explore how to use Redis as a data cache for a Spring Boot microservice using PostgreSQL as the database. Idea is to...
  • Python FastAPI microservice with Okta and OPA
    Authentication (AuthN) and Authorization (AuthZ) is a common challenge when developing microservices. In this article, we will explore how t...
  • Spring Boot with Okta and OPA
    Authentication (AuthN) and Authorization (AuthZ) is a common challenge when developing microservices. In this article, we will explore how t...
  • Getting started with Kafka in Python
    This article will provide an overview of Kafka and how to get started with Kafka in Python with a simple example. What is Kafka? ...
  • Getting started in GraphQL with Spring Boot
    In this article we will explore basic concepts on GraphQL and look at how to develop a microservice in Spring Boot with GraphQL support. ...

Copyright © StackStalk