0

I m a developper in a University (staff). We ve got a server that recently implemented Restful services and I trying to get ressources from those WS with a test project. I searched for 3 days and still blocked (i wouldn t ask for help otherwise...).

package org.o7planning;

import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

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 App 
{
    public static void main( String[] args ) throws JSONException {
        Integer codeApprenant = 1092914;
        String _URL = "https://WEB_SERV_IP_ADRESS/netypareotest/public/index.php/r/v1/preinscription/candidat/" + codeApprenant;
        String token = "mySecretToken";
    // Create JSON Object Component
    JSONObject obj_json_auth1 = new JSONObject();
    obj_json_auth1.put("X-Auth-Token", token);
    obj_json_auth1.put("Content-Type", "application/json");

    JSONObject obj_json_auth2 = new JSONObject();
    obj_json_auth2.put("CURLOPT_URL", _URL);
    obj_json_auth2.put("CURLOPT_HTTPHEADER", obj_json_auth1);
    obj_json_auth2.put("CURLOPT_RETURNTRANSFER", Boolean.TRUE);

    // Bare 
    String obj_json_auth_raw = "{" +
            "\"CURLOPT_URL\":\"" + _URL + "\"," +
            "\"CURLOPT_HTTPHEADER\":{" +
                "\"X-Auth-Token\":\"" + token + "\"," +
                "\"Content-Type\":\"application/json\"" +
                "}," +
            "\"CURLOPT_RETURNTRANSFER\":\""+ Boolean.TRUE + "\"" +
            "}";

    System.out.print("\n" + obj_json_auth2);
    System.out.print("\n" + obj_json_auth_raw);
    System.out.print("\n");

    // ----------------------------------------------------------------------------------------------------------
    try { // https://stackoverflow.com/questions/21404252/post-request-send-json-data-java-httpurlconnection
        URL object = new URL(_URL);

        HttpURLConnection con = (HttpURLConnection) object.openConnection();
        con.setConnectTimeout(5000);
        con.setDoOutput(true);
        con.setDoInput(true);
        con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
        con.setRequestMethod("GET");

        OutputStream os = con.getOutputStream();
        os.write(obj_json_auth_raw.toString().getBytes("UTF-8"));
        os.close();

        System.out.println("\n\n-----------" + con.getResponseCode());

        // Doesn't seem to work
        //OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream());
        //wr.write(obj_json_auth_raw.toString());
        //wr.flush();


        //display what returns the POST request
        StringBuilder sb = new StringBuilder();
        int HttpResult = con.getResponseCode();
        if (HttpResult == HttpURLConnection.HTTP_OK) {
            BufferedReader br = new BufferedReader(
                    new InputStreamReader(con.getInputStream(), "utf-8"));
            String line = null;
            while ((line = br.readLine()) != null) {
                sb.append(line + "\n");
            }
            br.close();
            System.out.println("" + sb.toString());
        } else {
            System.out.println(con.getResponseMessage());
        }

    } catch(IOException e) {
        e.printStackTrace();
    }
}}

I get this Exception : Code indicated as an issue

Output

I got a documentation with but it's in french so good luck to understand. By the way in it they left an example of authentication with PHP. (Below)

<?php
try {
 $baseUrl = "https://localhost/netypareo/public/index.php";
 $jeton = "token";
 // REQUÊTE CONSULTATION
 $url = $baseUrl . "/url de l'action";
 // options de la session
 $options = [
 CURLOPT_URL => $url,
 CURLOPT_HTTPHEADER => [
 "X-Auth-Token: " . $jeton,
 "Content-Type: application/json"
 ],
 CURLOPT_RETURNTRANSFER => true
 ];
// initialisation de la session
$ch = curl_init();
// configuration de la session
curl_setopt_array($ch, $options);
// exécution de la requête
$response = curl_exec($ch);
// fermeture de la session
curl_close($ch);
// affiche les données au format tableau
$data = json_decode($response, true);
 pArray($data);
}
catch (RestException $e) {
 echo $e;
}

Thank you by advance

0 Answers0