package com.dw.lib;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map.Entry;
import java.util.Set;

import com.dw.lib.FileUtils;
import com.dw.lib.JsonOp;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

public class HttpPostHelper {
	
	public void Post(String configPath, String outputFilePath) {
		try {
			if(outputFilePath == null) {
				outputFilePath = "output.txt";
			}
			FileUtils utils = new FileUtils();
			String config = utils.fileToString(configPath);
		
			JsonParser parser = new JsonParser();
			JsonObject data = parser.parse(config).getAsJsonObject();
			
			HttpURLConnection connection = null;
			String urlStr = data.get("url").getAsString();
			String method = data.get("method").getAsString();
			JsonObject headers = data.get("headers").getAsJsonObject();
			JsonObject body = data.get("body").getAsJsonObject();
			
			URL url = new URL(urlStr);
			connection = (HttpURLConnection) url.openConnection();

			// set http connection attribute
			
			connection.setRequestMethod(method.toUpperCase());
			
			if (!method.toUpperCase().equals("GET")) {
				connection.setDoOutput(true);
				connection.setDoInput(true);
				 // GET、POST、DELETE、INPUT
				connection.setUseCaches(false);
				connection.setInstanceFollowRedirects(true);
			}
			
			Set<Entry<String, JsonElement>> entrySet = headers.entrySet();
			
			for(Entry<String,JsonElement> entry : entrySet){
				connection.setRequestProperty(entry.getKey(), entry.getValue().getAsString()); // set require format to json
			}
			
			connection.connect();
			
			if (!method.toUpperCase().equals("GET")) {
				OutputStream out = connection.getOutputStream();
				out.write(body.toString().getBytes());
				out.flush();
				out.close();
			}
			
			BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
			String lines;
			StringBuffer sb = new StringBuffer("");
			while ((lines = reader.readLine()) != null) {
				lines = new String(lines.getBytes(), "utf-8");
				sb.append(lines);
			}
			
			JsonOp jsonOp = new JsonOp();
			String outStr = jsonOp.JsonFormater(sb.toString());
			System.out.println(outStr);
			utils.stringToFile(outStr, outputFilePath);
			
			reader.close();
			connection.disconnect();

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

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}

@@ -65,32 +65,60 @@
 io.IOException;%0A
+import java.io.InputStream;%0A
 import java.io.I
@@ -1339,16 +1339,49 @@
 ase());%0A
+%09%09%09connection.setDoOutput(true);%0A
 %09%09%09%0A%09%09%09i
@@ -1426,42 +1426,8 @@
 ) %7B%0A
-%09%09%09%09connection.setDoOutput(true);%0A
 %09%09%09%09
@@ -2037,16 +2037,293 @@
 %09%09%7D%0A%09%09%09%0A
+%09%09%09InputStream inputStream;%0A%0A            int status = connection.getResponseCode();%0A%0A            if (status != HttpURLConnection.HTTP_OK)%0A                inputStream = connection.getErrorStream();%0A            else%0A                inputStream = connection.getInputStream();%0A%09%09%09%0A
 %09%09%09Buffe
@@ -2382,31 +2382,17 @@
 mReader(
-connection.getI
+i
 nputStre
@@ -2393,18 +2393,16 @@
 utStream
-()
 ));%0A%09%09%09S

handle error as output stream