Skip to content

Commit 87776ca

Browse files
committed
HttpExecutor post method improvements
1 parent 0a27cf6 commit 87776ca

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

src/main/java/io/api/etherscan/executor/impl/HttpExecutor.java

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
import io.api.etherscan.util.BasicUtils;
77

88
import java.io.BufferedReader;
9-
import java.io.DataOutputStream;
109
import java.io.IOException;
1110
import java.io.InputStreamReader;
11+
import java.io.OutputStream;
1212
import java.net.HttpURLConnection;
1313
import java.net.SocketTimeoutException;
1414
import java.net.URL;
15+
import java.nio.charset.StandardCharsets;
1516
import java.util.HashMap;
1617
import java.util.Map;
1718
import java.util.zip.GZIPInputStream;
@@ -103,14 +104,16 @@ public String get(final String urlAsString) {
103104
public String post(final String urlAsString, final String dataToPost) {
104105
try {
105106
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());
108111

109112
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+
}
114117

115118
final int status = connection.getResponseCode();
116119
if (status == HTTP_MOVED_TEMP || status == HTTP_MOVED_PERM) {
@@ -141,13 +144,13 @@ private String readData(final HttpURLConnection connection) throws IOException {
141144
}
142145

143146
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+
}
152155
}
153156
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy