|
6 | 6 | importio.api.etherscan.util.BasicUtils; |
7 | 7 |
|
8 | 8 | importjava.io.BufferedReader; |
9 | | -importjava.io.DataOutputStream; |
10 | 9 | importjava.io.IOException; |
11 | 10 | importjava.io.InputStreamReader; |
| 11 | +importjava.io.OutputStream; |
12 | 12 | importjava.net.HttpURLConnection; |
13 | 13 | importjava.net.SocketTimeoutException; |
14 | 14 | importjava.net.URL; |
| 15 | +importjava.nio.charset.StandardCharsets; |
15 | 16 | importjava.util.HashMap; |
16 | 17 | importjava.util.Map; |
17 | 18 | importjava.util.zip.GZIPInputStream; |
@@ -103,14 +104,16 @@ public String get(final String urlAsString) { |
103 | 104 | publicStringpost(finalStringurlAsString,finalStringdataToPost) { |
104 | 105 | try { |
105 | 106 | finalHttpURLConnectionconnection =buildConnection(urlAsString,"POST"); |
106 | | -finalStringcontentLength = (BasicUtils.isEmpty(dataToPost)) ?"0" :String.valueOf(dataToPost.length()); |
107 | | -connection.setRequestProperty("content-length",contentLength); |
| 107 | +finalStringcontentLength = (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 | | -DataOutputStreamwr =newDataOutputStream(connection.getOutputStream()); |
111 | | -wr.writeBytes(dataToPost); |
112 | | -wr.flush(); |
113 | | -wr.close(); |
| 113 | +connection.connect(); |
| 114 | +try (OutputStreamos =connection.getOutputStream()) { |
| 115 | +os.write(dataToPost.getBytes(StandardCharsets.UTF_8)); |
| 116 | +} |
114 | 117 |
|
115 | 118 | finalintstatus =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 | privateInputStreamReadergetStreamReader(finalHttpURLConnectionconnection)throwsIOException { |
144 | | -finalbooleanhaveEncoding =connection.getContentEncoding() !=null; |
145 | | - |
146 | | -if (haveEncoding &&"gzip".equals(connection.getContentEncoding())) |
147 | | -returnnewInputStreamReader(newGZIPInputStream(connection.getInputStream()),"utf-8"); |
148 | | -elseif (haveEncoding &&"deflate".equals(connection.getContentEncoding())) |
149 | | -returnnewInputStreamReader(newInflaterInputStream(connection.getInputStream()),"utf-8"); |
150 | | -else |
151 | | -returnnewInputStreamReader(connection.getInputStream(),"utf-8"); |
| 147 | +switch (String.valueOf(connection.getContentEncoding())) { |
| 148 | +case"gzip": |
| 149 | +returnnewInputStreamReader(newGZIPInputStream(connection.getInputStream()),"utf-8"); |
| 150 | +case"deflate": |
| 151 | +returnnewInputStreamReader(newInflaterInputStream(connection.getInputStream()),"utf-8"); |
| 152 | +default: |
| 153 | +returnnewInputStreamReader(connection.getInputStream(),"utf-8"); |
| 154 | +} |
152 | 155 | } |
153 | 156 | } |