Sunday, 3 September 2017

Adding headers using HttpURLConnection

HttpURLConnection class provides 'setRequestProperty' method to set the headers.

Below step-by-step procedure explains how to set headers.

Step 1: Get HttpURLConnection object
                 URL url = new URL(urlToConnect);
                 HttpURLConnection httpUrlConnection = (HttpURLConnection) url.openConnection();

Step 2: Add headers to the HttpURLConnection using setRequestProperty method.
                 Map<String, String> headers = new HashMap<>();

                 headers.put("X-CSRF-Token", "fetch");
                 headers.put("content-type", "application/json");
                
                 for (String headerKey : headers.keySet()) {
                          httpUrlConnection.setRequestProperty(headerKey, headers.get(headerKey));
                 }
                
Find below working application.

Test.java
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

public class Test {

 private static HttpURLConnection getURLConnection(String urlToConnect) throws IOException {
  URL url = new URL(urlToConnect);
  HttpURLConnection httpUrlConnection = (HttpURLConnection) url.openConnection();
  return httpUrlConnection;
 }

 private static void setHeaders(HttpURLConnection httpUrlConnection, Map<String, String> headers) {
  for (String headerKey : headers.keySet()) {
   httpUrlConnection.setRequestProperty(headerKey, headers.get(headerKey));
  }
 }

 public static void main(String[] args) throws IOException {
  String url = "urlToConnect";
  HttpURLConnection httpUrlConnection = getURLConnection(url);

  Map<String, String> headers = new HashMap<>();

  headers.put("X-CSRF-Token", "fetch");
  headers.put("content-type", "application/json");

  setHeaders(httpUrlConnection, headers);
  httpUrlConnection.getContent();

 }

}



Previous                                                 Next                                                 Home

Get headers from url using HttpURLConnection

Below step by step procedure explains, how to get all headers from http url.

Step 1: Get HttpURLConnection instance from url.
                 URL url = new URL(urlToConnect);
                 HttpURLConnection httpUrlConnection = (HttpURLConnection) url.openConnection();
                 return httpUrlConnection;
                
Step 2: Use 'getHeaderFields' method of HttpURLConnection instance to get all the headers.
                 Map<String, List<String>> headers = httpURLConnection.getHeaderFields();

                 for (String headerName : headers.keySet()) {
                          System.out.println(headerName + " : " + headers.get(headerName));
                 }
                
Find below working application.

Test.java
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;

public class Test {

 private static HttpURLConnection getURLConnection(String urlToConnect) throws IOException {
  URL url = new URL(urlToConnect);
  HttpURLConnection httpUrlConnection = (HttpURLConnection) url.openConnection();
  return httpUrlConnection;
 }

 private static void printHeaders(HttpURLConnection httpURLConnection) {
  Map<String, List<String>> headers = httpURLConnection.getHeaderFields();

  for (String headerName : headers.keySet()) {
   System.out.println(headerName + " : " + headers.get(headerName));
  }
 }

 public static void main(String[] args) throws IOException {
  String url = "https://self-learning-java-tutorial.blogspot.com/2016/05/java-home-page.html";
  HttpURLConnection httpUrlConnection = getURLConnection(url);
  printHeaders(httpUrlConnection);

 }

}


Output
Transfer-Encoding : [chunked]
null : [HTTP/1.1 200 OK]
Alt-Svc : [quic=":443"; ma=2592000; v="39,38,37,35"]
Server : [GSE]
X-Content-Type-Options : [nosniff]
Last-Modified : [Thu, 31 Aug 2017 17:49:01 GMT]
Date : [Fri, 01 Sep 2017 09:50:33 GMT]
Accept-Ranges : [none]
Cache-Control : [private, max-age=0]
Vary : [Accept-Encoding]
Expires : [Fri, 01 Sep 2017 09:50:33 GMT]
X-XSS-Protection : [1; mode=block]
Content-Type : [text/html; charset=UTF-8]



Previous                                                 Next                                                 Home

Read URL Contents using HttpURLConnection

Follow below steps to read the contents from an url.

Step 1: Create a connection to the url.
                 URL url = new URL(urlToConnect);
                 HttpURLConnection httpUrlConnection = (HttpURLConnection) url.openConnection();
                
Step 2: Create input stream from the url connection.

                 int responseCode = httpUrlConnection.getResponseCode();
                 InputStream inputStream = null;

                 if (responseCode >= 200 && responseCode < 400) {
                          inputStream = httpUrlConnection.getInputStream();
                 } else {
                          inputStream = httpUrlConnection.getErrorStream();
                 }

Step 3: Read the content from input stream and print them to console.

                 BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));

                 String line = null;

                 while ((line = br.readLine()) != null) {
                          System.out.println(line);
                 }


Find below working application.


Test.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class Test {

 private static HttpURLConnection getURLConnection(String urlToConnect) throws IOException {
  URL url = new URL(urlToConnect);
  HttpURLConnection httpUrlConnection = (HttpURLConnection) url.openConnection();
  return httpUrlConnection;
 }

 private static InputStream getContent(HttpURLConnection httpUrlConnection) throws IOException {
  int responseCode = httpUrlConnection.getResponseCode();
  InputStream inputStream = null;

  if (responseCode >= 200 && responseCode < 400) {
   inputStream = httpUrlConnection.getInputStream();
  } else {
   inputStream = httpUrlConnection.getErrorStream();
  }

  return inputStream;
 }

 private static void printInputStream(InputStream inputStream) throws IOException {
  BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));

  String line = null;

  while ((line = br.readLine()) != null) {
   System.out.println(line);
  }
 }

 public static void main(String[] args) throws IOException {
  String url = "https://self-learning-java-tutorial.blogspot.com/2016/05/java-home-page.html";
  HttpURLConnection httpUrlConnection = getURLConnection(url);
  InputStream inputStream = getContent(httpUrlConnection);
  printInputStream(inputStream);

 }

}



Previous                                                 Next                                                 Home

Working with HttpURLConnection class

HttpURLConnection class is used to establish a connection to http end points. By using HttpURLConnection class, you can open a connection to given http end point, read the content, write content, read and write headers, read response status codes etc., In this tutorial series, you will get hands on of all these things.

·       Read URL Contents using HttpURLConnection
·       Get headers from url using HttpURLConnection
·       Adding headers using HttpURLConnection
·       Post data to url using HttpURLConnection



Previous                                                 Next                                                 Home

Saturday, 2 September 2017

ABAP: Comments

Comments are used to document the ABAP application. There are two kinds of comments.

a.   Full line comments
b.   Partial line comments

Full line comments
Full line comments are start with *.

Example
* I used to document the code.

ZHELLO_WORLD
*---------------------------------------------------------------------
* Report zhello_world
*---------------------------------------------------------------------
*
*---------------------------------------------------------------------

REPORT zhello_world.

Write: 'Hello World'.

Partial line comments
Partial line comments starts with " (Double quote).


ZHELLO_WORLD
*---------------------------------------------------------------------
* Report zhello_world
*---------------------------------------------------------------------
*
*---------------------------------------------------------------------

REPORT zhello_world.

Write: 'Hello World'. "I am partial line comment




Previous                                                 Next                                                 Home

Friday, 1 September 2017

ABAP: Statements

Every statement in ABAP starts with ABAP keyword and ends with a '.'.

If your program is an executable, then the first non-comment line should be 'REPORT REPORT_NAME'. If your program is not executable, then the first non-comment line should be 'PROGRAM PROGRAM_NAME'.

ZHELLO_WORLD
*&---------------------------------------------------------------------*
*& Report zhello_world
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zhello_world.

Write 'Hello World'.

Colon Notation
If the statements are identical, then consecutive statements can be chained using : notation.

Write 'Hello'.
Write 'Good'.
Write 'Morning'.

Above statements can be chained like below.

Write: 'Hello', 'Good', 'Morning'.


ZHELLO_WORLD
*&---------------------------------------------------------------------*
*& Report zhello_world
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zhello_world.

Write: 'Hello', 'Good', 'Morning'.



Previous                                                 Next                                                 Home

ABAP Editor : Hello World Program

ABAP Editor is used to create, edit and run the programs.

How to open ABAP Editor?
By navigating through the Folder structure on SAP Easy Access Pane (or) by entering the transaction ‘SE38’ in command field.

Navigating through folder structure.
Tools -> ABAP Workbench -> Development -> ABAP Editor

By using transaction technical name
You can open ABAP Editor by entering the transaction technical name ‘SE38’ in the command field.
Since we are going to use ABAP editor throughout the tutorial, add this as favorite. I already explained in my previous post ‘How to add a Favorite?’.

On you entered the transaction ‘SE38’ in command window, it opens the window like below.

Below image summarizes the important sections of ABAP Editor

Create Hello World Program
Enter the name of the program as ‘Z_HELLO_WORLD’ and press Create button.
All the custom programs must start with the letter ‘Z’ (or) ‘Y’.
Once you press ‘Create’ button, it opens below window.  Give some meaningful title to the application and select the type as ‘Executable Program’ and press Save button.

Once you press the Save button, it opens below window.
Click the ‘Local Object’. It opens the Editor.

As you see, by default program is in Inactive state. Update the program like below.

Z_Hello_World
*&---------------------------------------------------------------------*
*& Report Z_HELLO_WORLD
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT Z_HELLO_WORLD.

Write 'Hello World'.

Save the program by using CTRL + S. Activate the program and run the program.
First check the program for any syntax errors. If there are no syntax error, you will get ‘No Syntax Errors found’ message at the bottom of the window.
Now activate the program by clicking Activate button.
Now you can run the program. Once you run the program, you can able to see below window.
As you observe the title we had given is displayed in the title bar of the window and the content of Write statement is displayed in body of the window.

That’s it. You are done with the first steps of ABAP Programming. It is time to explore more about ABAP syntax.
Previous                                                 Next                                                 Home