Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
apollo
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
openSource
apollo
Commits
6633f688
Unverified
Commit
6633f688
authored
Dec 16, 2017
by
Jason Song
Committed by
GitHub
Dec 16, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #883 from nobodyiam/refactor-httputil-error-handling-merge
refactor httputil error stream handling
parents
f9cb281d
aec394a6
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
29 additions
and
17 deletions
+29
-17
HttpUtil.java
...n/java/com/ctrip/framework/apollo/util/http/HttpUtil.java
+29
-17
No files found.
apollo-client/src/main/java/com/ctrip/framework/apollo/util/http/HttpUtil.java
View file @
6633f688
...
...
@@ -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
e
x
)
{
// ignore
}
}
if
(
esr
!=
null
)
{
try
{
CharStreams
.
toString
(
esr
);
esr
.
close
();
}
catch
(
Exception
e
)
{
// ignore
}
try
{
esr
.
close
();
}
catch
(
IOException
ex
)
{
// ignore
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment