import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.HashMap; public class EcoCostClient { static String EcoCostParamURL = null; static String EcoCostURL = null; static String TPRateURL = null; static String reqFileName; static String type; static String ServiceName; static HashMap urlList = new HashMap(); public static String readFile(String fileName) throws IOException { System.out.println("Reading input params from file :" + fileName); BufferedReader rd = new BufferedReader(new FileReader(fileName)); StringBuilder sb = new StringBuilder(); String line = ""; while ((line = rd.readLine()) != null) { // System.out.println(line); sb.append(line); } return sb.toString(); } public static void main(String[] args) { // TODO Auto-generated method stub String TYPE = null; System.out.println("Args length" + args.length); if (args.length == 2) { reqFileName = args[0]; ServiceName = args[1]; } else { System.out.println("Invalid argument passed ...!"); System.out.println("Usage: "); System.exit(1); } System.out.println(" Request URL :" + ServiceName); System.out.println(" Request Json File :" + reqFileName); String URL = ServiceName; if (URL == null) { System.out.println("Unable to find URL from input"); System.exit(4); } try { String request = readFile(reqFileName); System.out.println("\nRequest: " + request); callService(URL, request); } catch (Exception e) { e.printStackTrace(); } } public static void callService(String url1, String inputString) { try { URL url = new URL(url1); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/json"); String input = inputString; OutputStream os = conn.getOutputStream(); os.write(input.getBytes()); os.flush(); InputStream _is; if (conn.getResponseCode() < HttpURLConnection.HTTP_BAD_REQUEST) { _is = conn.getInputStream(); } else { /* error from server */ _is = conn.getErrorStream(); } BufferedReader br = new BufferedReader(new InputStreamReader(_is)); String output; System.out.println("Output from Server .... \n"); while ((output = br.readLine()) != null) { System.out.println(output); } conn.disconnect(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }