|
6 | 6 | import io.api.etherscan.util.BasicUtils; |
7 | 7 |
|
8 | 8 | import java.io.BufferedReader; |
9 | | -import java.io.DataOutputStream; |
10 | 9 | import java.io.IOException; |
11 | 10 | import java.io.InputStreamReader; |
| 11 | +import java.io.OutputStream; |
12 | 12 | import java.net.HttpURLConnection; |
13 | 13 | import java.net.SocketTimeoutException; |
14 | 14 | import java.net.URL; |
| 15 | +import java.nio.charset.StandardCharsets; |
15 | 16 | import java.util.HashMap; |
16 | 17 | import java.util.Map; |
17 | 18 | import java.util.zip.GZIPInputStream; |
@@ -103,14 +104,16 @@ public String get(final String urlAsString) { |
103 | 104 | public String post(final String urlAsString, final String dataToPost) { |
104 | 105 | try { |
105 | 106 | final HttpURLConnection connection = buildConnection(urlAsString, "POST"); |
106 | | - final String contentLength = (BasicUtils.isEmpty(dataToPost)) ? "0" : String.valueOf(dataToPost.length()); |
107 | | - connection.setRequestProperty("content-length", contentLength); |
| 107 | + final String contentLength = (BasicUtils.isBlank(dataToPost)) ? "0" : String.valueOf(dataToPost.length()); |
| 108 | + connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); |
| 109 | + connection.setRequestProperty("Content-Length", contentLength); |
| 110 | + connection.setFixedLengthStreamingMode(dataToPost.length()); |
108 | 111 |
|
109 | 112 | connection.setDoOutput(true); |
110 | | - DataOutputStream wr = new DataOutputStream(connection.getOutputStream()); |
111 | | - wr.writeBytes(dataToPost); |
112 | | - wr.flush(); |
113 | | - wr.close(); |
| 113 | + connection.connect(); |
| 114 | + try (OutputStream os = connection.getOutputStream()) { |
| 115 | + os.write(dataToPost.getBytes(StandardCharsets.UTF_8)); |
| 116 | + } |
114 | 117 |
|
115 | 118 | final int status = connection.getResponseCode(); |
116 | 119 | if (status == HTTP_MOVED_TEMP || status == HTTP_MOVED_PERM) { |
@@ -141,13 +144,13 @@ private String readData(final HttpURLConnection connection) throws IOException { |
141 | 144 | } |
142 | 145 |
|
143 | 146 | private InputStreamReader getStreamReader(final HttpURLConnection connection) throws IOException { |
144 | | - final boolean haveEncoding = connection.getContentEncoding() != null; |
145 | | - |
146 | | - if (haveEncoding && "gzip".equals(connection.getContentEncoding())) |
147 | | - return new InputStreamReader(new GZIPInputStream(connection.getInputStream()), "utf-8"); |
148 | | - else if (haveEncoding && "deflate".equals(connection.getContentEncoding())) |
149 | | - return new InputStreamReader(new InflaterInputStream(connection.getInputStream()), "utf-8"); |
150 | | - else |
151 | | - return new InputStreamReader(connection.getInputStream(), "utf-8"); |
| 147 | + switch (String.valueOf(connection.getContentEncoding())) { |
| 148 | + case "gzip": |
| 149 | + return new InputStreamReader(new GZIPInputStream(connection.getInputStream()), "utf-8"); |
| 150 | + case "deflate": |
| 151 | + return new InputStreamReader(new InflaterInputStream(connection.getInputStream()), "utf-8"); |
| 152 | + default: |
| 153 | + return new InputStreamReader(connection.getInputStream(), "utf-8"); |
| 154 | + } |
152 | 155 | } |
153 | 156 | } |
0 commit comments