1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | package naverpay; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.net.URL; import java.net.URLEncoder; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSession; import javax.net.ssl.TrustManager; import org.json.simple.JSONArray; import org.json.simple.JSONObject; public class HttpPost { public static void main(String[] args) { HttpPost obj = new HttpPost(); obj.getHttpsConnection(); } public JSONObject getMakeJsonData() { JSONObject object = new JSONObject(); object.put("modelVersion", "2"); object.put("merchantUserKey", ""); object.put("merchantPayKey", ""); object.put("productName", "테스트상품"); object.put("productCount", 1); object.put("totalPayAmount", 1000); object.put("returnUrl", "http://localhost:8080/naverpay/"); object.put("taxScopeAmount", 1000); object.put("taxExScopeAmount", 0); object.put("purchaserName", "구매자"); JSONArray list = new JSONArray(); JSONObject productItem = new JSONObject(); productItem.put("categoryType", "INSURANCE"); productItem.put("categoryId", "TRAVELER"); productItem.put("uid", "005"); productItem.put("name", "테스트상품"); productItem.put("payReferrer", "ETC"); productItem.put("count", 1); list.add(productItem); object.put("productItems", list); return object; } public String UrlEncodeUTF8(String s) { try{ return URLEncoder.encode(s, "UTF-8"); } catch(UnsupportedEncodingException ex) { throw new UnsupportedOperationException(ex); } } public void getHttpsConnection() { HttpsURLConnection conn = null; try { URL url = new URL("https://dev.apis.naver.com/partner-id/naverpay/payments/v2/reserve"); conn = (HttpsURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setRequestProperty("X-Naver-Client-Id", ""); conn.setRequestProperty("X-Naver-Client-Secret", ""); conn.setRequestProperty("Content-Type", "application/json"); conn.setRequestProperty("charset", "UTF-8"); conn.setDoOutput(true); conn.setUseCaches(false); conn.setConnectTimeout(10000); conn.setReadTimeout(10000); conn.setHostnameVerifier(new HostnameVerifier() { @Override public boolean verify(String hostname, SSLSession session) { return true; } }); // SSL setting SSLContext context = SSLContext.getInstance("TLS"); context.init(null, new TrustManager[]{ new javax.net.ssl.X509TrustManager() { @Override public X509Certificate[] getAcceptedIssuers() { return null; } @Override public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { } @Override public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { } } }, null); conn.setSSLSocketFactory(context.getSocketFactory()); // Connect to host conn.connect(); conn.setInstanceFollowRedirects(true); String param = this.getMakeJsonData().toJSONString(); OutputStream wr1 = conn.getOutputStream(); DataOutputStream wr = new DataOutputStream(conn.getOutputStream()); wr1.write(param.getBytes("UTF-8")); wr1.flush(); wr1.close(); final int responseCode = conn.getResponseCode(); InputStream in = conn.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); final StringBuffer buffer = new StringBuffer(); String line = null; while ((line = reader.readLine()) != null) { buffer.append(line); } reader.close(); System.out.println("##########"); System.out.println("##########"); System.out.println("##########"); System.out.println("##########"); System.out.println("##########"); System.out.println("result ::: "+buffer.toString()); } catch (Exception e) { e.printStackTrace(); } finally { if(conn != null) { conn.disconnect(); } } } } | cs |
네이버페이 결제예약호출 예제시 400에러가 발생하여 해당코드를 위와같이 수정하였다.
json post 전송시 DataOutputStream 을 통해 전송하였으며 21라인에 UTF-8로 반드시 인코딩해서 데이터를 넘겨준다.
'실무이야기' 카테고리의 다른 글
Javascript call by value, call by reference (0) | 2021.03.22 |
---|---|
Java DecimalFormat 소수점 표시 (0) | 2021.03.21 |
운영내용 작성할내용 카테고리 (0) | 2019.10.20 |
모바일웹 네이버페이 연동후기 (0) | 2019.06.08 |
userAgent 스크립트로 모바일웹 페이지로 이동하기 (0) | 2018.10.18 |