未验证 提交 6633f688 编写于 作者: J Jason Song 提交者: GitHub

Merge pull request #883 from nobodyiam/refactor-httputil-error-handling-merge

refactor httputil error stream handling
......@@ -8,6 +8,7 @@ import com.google.common.base.Function;
import com.google.common.io.CharStreams;
import com.google.gson.Gson;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.net.HttpURLConnection;
......@@ -93,20 +94,33 @@ public class HttpUtil {
conn.connect();
statusCode = conn.getResponseCode();
String response;
try {
isr = new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8);
} catch (Exception e) {
// ignore
}
try {
esr = new InputStreamReader(conn.getErrorStream(), StandardCharsets.UTF_8);
} catch (Exception e) {
// ignore
response = CharStreams.toString(isr);
} catch (IOException ex) {
/**
* according to https://docs.oracle.com/javase/7/docs/technotes/guides/net/http-keepalive.html,
* we should clean up the connection by reading the response body so that the connection
* could be reused.
*/
InputStream errorStream = conn.getErrorStream();
if (errorStream != null) {
esr = new InputStreamReader(errorStream, StandardCharsets.UTF_8);
try {
CharStreams.toString(esr);
} catch (IOException ioe) {
//ignore
}
}
throw ex;
}
if (statusCode == 200) {
String content = CharStreams.toString(isr);
return new HttpResponse<>(statusCode, serializeFunction.apply(content));
return new HttpResponse<>(statusCode, serializeFunction.apply(response));
}
if (statusCode == 304) {
......@@ -117,20 +131,18 @@ public class HttpUtil {
} finally {
if (isr != null) {
try {
CharStreams.toString(isr);
isr.close();
} catch (IOException e) {
} catch (IOException ex) {
// ignore
}
}
if (esr != null) {
try {
CharStreams.toString(esr);
esr.close();
} catch (Exception e) {
// ignore
}
try {
esr.close();
} catch (IOException ex) {
// ignore
}
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册