Call_javanet.java [src/java/client/example] Revision:   Date:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package client.example;

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

/**
 *
 * @author od
 */
public class Call_javanet {

    public static void main(String[] args) throws Exception {
        URL url = new URL("http://localhost:8080/csip-example/m/simpleservice/1.0");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setUseCaches(false);
        connection.setDoInput(true);
        connection.setDoOutput(true);
        OutputStream os = connection.getOutputStream();
        os.write(("{\n"
                + "    \"metainfo\": {\n"
                + "    },\n"
                + "    \"parameter\": [ \n"
                + "     {\n"
                + "      \"name\": \"temp\",\n"
                + "      \"value\": 25\n"
                + "     }\n"
                + "    ]\n"
                + "}").getBytes());
        os.flush();
        os.close();

        if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
            throw new RuntimeException("Failed.");
        }
        InputStream is = connection.getInputStream();
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));
        StringBuilder response = new StringBuilder();
        String line;
        while ((line = rd.readLine()) != null) {
            response.append(line);
            response.append('\n');
        }
        System.out.println("Response: " + response.toString());
        connection.disconnect();
    }

}