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.
Email ThisBlogThis!Share to XShare to Facebook
Newer Post Older Post Home

0 comments:

Post a Comment

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

Popular Posts

  • Python FastAPI file upload and download
    In this article, we will look at an example of how to implement a file upload and download API in a Python FastAPI microservice. Example bel...
  • 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 ...
  • Accessing the Kubernetes API
    In this article, we will explore the steps required to access the Kubernetes API and overcome common challenges. All operations and communic...
  • 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...
  • Scheduling jobs in Python
    When developing applications and microservices we run into scenarios where there is a need to run scheduled tasks. Examples include performi...
  • Using Tekton to deploy KNative services
    Tekton is a popular open-source framework for building continuous delivery pipelines. Tekton provides a declarative way to define pipelines ...

Copyright © StackStalk