[JAVA] POST 로 json 데이터 전송하기

[JAVA] POST 로 json 데이터 전송하기

json 데이터가 아닌 일반 파라미터로 POST 호출하는 방식은 다음 포스트 참고 : https://blog.naver.com/bb_/222539952177

 

    public String sendJsonPost(String surl, String encode, String jsonString) throws IOException, Exception {
        if (encode == null || encode.length() == 0) {
            encode = “UTF-8”;
        }

        String result = “”;

        HttpURLConnection ucon = null;
        InputStreamReader isr = null;
        InputStream is = null;
        BufferedReader br = null;

        try {
            URL url = new URL(surl);
            ucon = (HttpURLConnection) url.openConnection();

            ucon.setDoOutput(true);
            ucon.setRequestProperty(“Content-Type”“application/json”);
            ucon.setConnectTimeout(3 * 1000);
            ucon.setReadTimeout(3 * 1000);
            ucon.setUseCaches(false);

            if (jsonString != null && jsonString.length() > 0) {
                OutputStream os = null;
                OutputStreamWriter writer = null;
                try {
                    os = ucon.getOutputStream();
                    writer = new OutputStreamWriter(os);
                    writer.write(jsonString);
                    writer.close();
                    os.close();

                } catch (IOException e) {
                    throw e;

                } catch (Exception e) {
                    throw e;

                } finally {
                    close(writer);
                    close(os);
                }
            }

            ucon.connect();

            is = ucon.getInputStream();
            isr = new InputStreamReader(is, encode);
            br = new BufferedReader(isr);

            StringBuffer resultBuff = new StringBuffer();

            String line = “”;
            while ((line = br.readLine()) != null) {
                resultBuff.append(line);
            }

            result = resultBuff.toString();

        } catch (IOException e) {
            throw e;

        } catch (Exception e) {
            throw e;

        } finally {
            close(br);
            close(isr);
            close(is);
            disconnect(ucon);
        }

        return result;
    }

    public void close(OutputStreamWriter writer) {
        try {
            if (writer != null) {
                writer.close();
            }
        } catch (IOException e) {
        } catch (Exception e) {
        } finally {
            writer = null;
        }
    }

    public void close(OutputStream os) {
        try {
            if (os != null) {
                os.close();
            }
        } catch (IOException e) {
        } catch (Exception e) {
        } finally {
            os = null;
        }
    }

    public void close(BufferedReader br) {
        try {
            if (br != null) {
                br.close();
            }
        } catch (IOException e) {
        } catch (Exception e) {
        } finally {
            br = null;
        }
    }

    public void close(InputStreamReader isr) {
        try {
            if (isr != null) {
                isr.close();
            }
        } catch (IOException e) {
        } catch (Exception e) {
        } finally {
            isr = null;
        }
    }

    public void close(InputStream is) {
        try {
            if (is != null) {
                is.close();
            }
        } catch (IOException e) {
        } catch (Exception e) {
        } finally {
            is = null;
        }
    }

    public void disconnect(HttpURLConnection ucon) {
        try {
            if (ucon != null) {
                ucon.disconnect();
            }
        } catch (NullPointerException e) {
        } catch (Exception e) {
        } finally {
            ucon = null;
        }
    }