Ewon TechForum
Providing technical solutions to technical requests


Using JAVA to send HTTPS POST and GET requests
AndreasABB Offline
Junior Member
#1
Hi,

I've been working with an eWON Flexy 205 for a short amount of time now. I have an application (Java 11) which obviously needs to be refactored to Java 1.4.4. So far I've managed to adapt. However, I cannot find any usefull information about how to send HTTPS POST and HTTPS GET requests. I've been told it's possible, but so far I've had no luck.

I've read all eWON Java pdf's I could find, as well as the BASIC User Guide.

I need to be able to read the HTTPS response as well.

Kind regards,
Andreas

TL;DR:
How to send HTTPS GET and POST requests and how to read them using java?
Reply

simon Offline
eWON Support
#2
Andreas,

The only way to do is to use the REQUESTHTTPX function (in ScheduleActionManager class).
This function allows you to customize the header and the body to send and you get the response into a file.

Simon
Reply

AndreasABB Offline
Junior Member
#3
Hi Simon,

First of all, thanks for the quick reply.

Second, is there an example somewhere that I could look into or an API of some sort regarding both using RequestHttpX() and retrieving data from a HTTPS Response?

Thanks in advance, 
Kind regards,
Andreas
Reply

simon Offline
eWON Support
#4
Andreas,

The function usage is the same as in BASIC except that the RESPONSEHTTPX is not implemented in JAVA.
So, you should have a look at the BASIC manual : https://developer.ewon.biz/content/basic-1

Simon
Reply

AndreasABB Offline
Junior Member
#5
Simon,

I've tried that, but I cannot figure out how it works.

Perhaps some code of what I'm trying to do helps:
Code:
try {
            java.net.URL url = new URL(URL);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", "application/json");
            connection.setDoOutput(true);

            connection.connect();
            //write request body with JSON
            OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream());

            wr.write(RequestJSONAuthBody);
            wr.close();

            System.out.println(connection.getResponseCode());

            //read response body
            InputStream in = connection.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            StringBuilder result = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                result.append(line);
            }
            connection.disconnect();

            JSONParser parser = new JSONParser();

            Object o = parser.parse(result.toString());
            JSONObject data = (JSONObject) o;
            authToken = (String) data.get("authToken");
            organizationID = (Long) data.get("organizationID");

        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
This is what I have accomplished with HttpUrlConnection, obviously I cannot use it on an eWON.

Thanks in advance,
Andreas
Reply

simon Offline
eWON Support
#6
Andreas,



The equivalent should be :

Code:
int status = com.ewon.ewonitf.ScheduledActionManager.RequestHttpX("https://web.com",
"POST",
"Content-Type=application/json",
"{data:test}",
"",
"/usr/response.txt");

if (status == 0)
{
            FileConnection File = (FileConnection) Connector.open("file:////usr/response.txt");
            if (!File.exists()) {
                Utils.realTimeLog("File " + Path + " not found", 1, param_logLevel);
                return;
            }

            InputStream reader = File.openInputStream();
            String line = null;

            while ((line = Utils.readLine(reader)) != null) {

                //Read Response
            }
            reader.close();
            File.close();
}

    public static String readLine(InputStream reader) throws IOException {
        // Test whether the end of file has been reached. If so, return null.
        int readChar = reader.read();
        if (readChar == -1) {
            return null;
        }
        StringBuffer string = new StringBuffer("");
        // Read until end of file or new line
        while (readChar != -1 && readChar != '\n') {
            if (readChar != '\r') {
                string.append((char)readChar);
            }
            // Read the next character
            readChar = reader.read();
        }
        return string.toString();
    }

Hope it helps

Simon
Reply

LewisH304 Offline
Junior Member
#7
Code:
package com.journaldev.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpURLConnectionExample {

    private static final String USER_AGENT = "Mozilla/5.0";

    private static final String GET_URL = "http://localhost:9090/SpringMVCExample";

    private static final String POST_URL = "http://localhost:9090/SpringMVCExample/home";

    private static final String POST_PARAMS = "userName=Pankaj";

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

        sendGET();
        System.out.println("GET DONE");
        sendPOST();
        System.out.println("POST DONE");
    }

    private static void sendGET() throws IOException {
        URL obj = new URL(GET_URL);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        con.setRequestMethod("GET");
        con.setRequestProperty("User-Agent", USER_AGENT);
        int responseCode = con.getResponseCode();
        System.out.println("GET Response Code :: " + responseCode);
        if (responseCode == HttpURLConnection.HTTP_OK) { // success
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    con.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            // print result
            System.out.println(response.toString());
        } else {
            System.out.println("GET request not worked");
        }

    }

    private static void sendPOST() throws IOException {
        URL obj = new URL(POST_URL);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        con.setRequestMethod("POST");
        con.setRequestProperty("User-Agent", USER_AGENT);

        // For POST only - START
        con.setDoOutput(true);
        OutputStream os = con.getOutputStream();
        os.write(POST_PARAMS.getBytes());
        os.flush();
        os.close();
        // For POST only - END

        int responseCode = con.getResponseCode();
        System.out.println("POST Response Code :: " + responseCode);

        if (responseCode == HttpURLConnection.HTTP_OK) { //success
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    con.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            // print result
            System.out.println(response.toString());
        } else {
            System.out.println("POST request not worked");
        }
    }

}
Try this code! Hope they help you!
Regards,
Lewis
Reply

simon Offline
eWON Support
#8
Hi Lewis,

The java.net.HttpURLConnection class is unfortunately not supported by our JVM.
You must use the proprietary function REQUESTHTTPX.

Simon
Reply



Possibly Related Threads…
  Java 1.4 update Started by tomT
12 Replies - 1.670 Views
24-03-2020, 09:08 PM
Last Post: hernfiry
13-03-2020, 01:12 PM
Last Post: simon
05-03-2020, 02:11 PM
Last Post: simon
03-03-2020, 11:35 PM
Last Post: simon
20-02-2020, 10:33 PM
Last Post: simon
  JAVA Azure IOTHub Connector Started by simon
1 Replies - 963 Views
11-12-2019, 12:19 PM
Last Post: simon
10-12-2019, 05:50 AM
Last Post: Kanduri Shouri
  Download Java ETK 1.4.3 Started by woodyjon
2 Replies - 374 Views
14-11-2019, 10:13 AM
Last Post: woodyjon
30-10-2019, 07:52 PM
Last Post: IceControls
05-09-2019, 05:17 PM
Last Post: simon



Users browsing this thread:
1 Guest(s)



Theme © Ewon 2019 - Forum software by © MyBB - Cookie policy