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; import sun.misc.BASE64Encoder; public class PricingClient { 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; String userId = null; String password=null; System.out.println("Args length" + args.length); if (args.length == 4) { reqFileName = args[0]; ServiceName = args[1]; userId = args[2]; password = args[3]; } 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,userId,password); } catch (Exception e) { e.printStackTrace(); } } public static void callService(String url1, String inputString, String userId, String pswd) { try { URL url = new URL(url1); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); String name = userId; String password = pswd; String authString = name + ":" + password; @SuppressWarnings("restriction") String authStringEnc = new BASE64Encoder().encode(authString.getBytes()); System.out.println("Base64 encoded auth string: " + authStringEnc); conn.setRequestProperty("Authorization", "Basic"+" "+ authStringEnc); conn.setRequestProperty("UserID",name); conn.setDoOutput(true); conn.setRequestMethod("POST"); if(url1.indexOf(("getPricingData")) !=-1) conn.setRequestMethod("GET"); 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(); } if (conn.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED) { System.out.println( "HTTP_UNAUTHORIZED"); } if(_is!= null){ 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(); } } }