/*
 * Copyright 2017-2017 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.hellojavaer.ddal.core.utils;

import java.io.*;
import java.net.*;
import java.util.Map;

/**
 *
 * @author <a href="mailto:hellojavaer@gmail.com">Kaiming Zou</a>,created on 09/09/2017.
 */
public class HttpUtils {

    private static final String USER_AGENT = "Mozilla/5.0";

    public static String sendPost(String url, Map<String, Object> params) {
        try {
            URL obj = new URL(url);
            HttpURLConnection con = (HttpURLConnection) obj.openConnection();
            con.setRequestMethod("POST");
            con.setRequestProperty("User-Agent", USER_AGENT);
            con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
            con.setDoOutput(true);
            // send
            DataOutputStream wr = null;
            try {
                wr = new DataOutputStream(con.getOutputStream());
                if (params != null && !params.isEmpty()) {
                    StringBuilder sb = new StringBuilder();
                    for (Map.Entry<String, Object> entry : params.entrySet()) {
                        sb.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
                        sb.append('=');
                        sb.append(URLEncoder.encode(entry.getValue().toString(), "UTF-8"));
                        sb.append('&');
                    }
                    sb.deleteCharAt(sb.length() - 1);
                    wr.write(sb.toString().getBytes("UTF-8"));
                }
                wr.flush();
            } finally {
                closeIO(wr);
            }
            // get
            BufferedReader in = null;
            try {
                int responseCode = con.getResponseCode();
                if (responseCode != 200) {
                    throw new IllegalStateException(responseCode + "");
                }
                in = new BufferedReader(new InputStreamReader(con.getInputStream()));
                String inputLine;
                StringBuilder response = new StringBuilder();
                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                }
                return response.toString();
            } finally {
                closeIO(in);
            }
        } catch (Exception e) {
            if (e instanceof RuntimeException) {
                throw (RuntimeException) e;
            } else {
                throw new RuntimeException(e);
            }
        }
    }

    private static void closeIO(Closeable closeable) {
        if (closeable != null) {
            try {
                closeable.close();
            } catch (IOException e) {
            }
        }
    }
}

@@ -2211,18 +2211,18 @@
 dReader 
-in
+br
  = null;
@@ -2238,204 +2238,240 @@
     
-try %7B%0A                int responseCode = con.getResponseCode();%0A                if (responseCode != 200) %7B%0A                    throw new IllegalStateException(responseCode + %22%22);%0A                %7D
+InputStream in;%0A            if (con.getResponseCode() %3C HttpURLConnection.HTTP_BAD_REQUEST) %7B%0A                in = con.getInputStream();%0A            %7D else %7B%0A                in = con.getErrorStream();%0A            %7D%0A            try %7B
 %0A   
@@ -2483,18 +2483,18 @@
         
-in
+br
  = new B
@@ -2529,36 +2529,18 @@
 mReader(
-con.getInputStream()
+in
 ));%0A    
@@ -2667,18 +2667,18 @@
 tLine = 
-in
+br
 .readLin
@@ -2738,16 +2738,23 @@
 nputLine
+ + %22%5Cn%22
 );%0A     
@@ -2758,32 +2758,273 @@
               %7D%0A
+                int responseCode = con.getResponseCode();%0A                if (responseCode != 200) %7B%0A                    throw new IllegalStateException(%22http code %22 + responseCode + %22%5Cn%22 + response.toString());%0A                %7D else %7B%0A    
                 
@@ -3047,24 +3047,42 @@
 toString();%0A
+                %7D%0A
             
@@ -3117,18 +3117,18 @@
 closeIO(
-in
+br
 );%0A     

update HttpUtils