package com.chenyang.nse.bussiness.controller.restfulapi;

import com.chenyang.nse.bussiness.controller.restfulapi.tools.Base64Tool;
import com.chenyang.nse.bussiness.tools.string.PasswordEncryptTool;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

public class RestUtil {
   public String load(String url, String query) throws Exception {
      URL restURL = new URL(url);
      HttpURLConnection conn = (HttpURLConnection)restURL.openConnection();
      conn.setRequestMethod("POST");
      String AuthorizationStr = "masking:" + PasswordEncryptTool.generatePassword("masking");
      AuthorizationStr = Base64Tool.encryptBASE64(AuthorizationStr);
      AuthorizationStr = URLEncoder.encode(AuthorizationStr, "UTF-8");
      conn.setDoOutput(true);
      conn.setDoInput(true);
      conn.setAllowUserInteraction(true);
      PrintStream ps = new PrintStream(conn.getOutputStream());
      ps.print(query);
      ps.close();
      BufferedReader bReader = new BufferedReader(new InputStreamReader(conn.getInputStream()));

      String line;
      String resultStr;
      for(resultStr = ""; null != (line = bReader.readLine()); resultStr = resultStr + line) {
      }

      bReader.close();
      conn.disconnect();
      return resultStr;
   }

   public static void main(String[] args) {
      try {
         RestUtil restUtil = new RestUtil();
         String serverIp = "192.168.2.100";
         String stopResultString = "";
         stopResultString = restUtil.load("http://" + serverIp + ":8080/ghca-platform/api/ft/se/0/pd/111/vn/402882ea7216b44101721709e9e80002/td/91c4b4a9f4394ed3904910800613127a/dd/ff8080816a04b48f016a053447c9000b/sa/ANONYMOUS/te/ZXC_01", "");
         System.out.println(stopResultString);
      } catch (Exception e) {
         System.out.print(e.getMessage());
      }

   }
}
