in stetho-sample/src/main/java/com/facebook/stetho/sample/Networker.java [97:147]
private HttpURLConnection configureAndConnectRequest() throws IOException {
URL url = new URL(request.url);
// Note that this does not actually create a new connection so it is appropriate to
// defer preConnect until after the HttpURLConnection instance is configured. Do not
// invoke connect, conn.getInputStream, conn.getOutputStream, etc before calling
// preConnect!
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
try {
conn.setReadTimeout(READ_TIMEOUT_MS);
conn.setConnectTimeout(CONNECT_TIMEOUT_MS);
conn.setRequestMethod(request.method.toString());
// Adding this disables transparent gzip compression so that we can intercept
// the raw stream and display the correct response body size.
requestDecompression(conn);
SimpleRequestEntity requestEntity = null;
if (request.body != null) {
requestEntity = new ByteArrayRequestEntity(request.body);
}
stethoManager.preConnect(conn, requestEntity);
try {
if (request.method == HttpMethod.POST) {
if (requestEntity == null) {
throw new IllegalStateException("POST requires an entity");
}
conn.setDoOutput(true);
requestEntity.writeTo(conn.getOutputStream());
}
// Ensure that we are connected after this point. Note that getOutputStream above will
// also connect and exchange HTTP messages.
conn.connect();
stethoManager.postConnect();
return conn;
} catch (IOException inner) {
// This must only be called after preConnect. Failures before that cannot be
// represented since the request has not yet begun according to Stetho.
stethoManager.httpExchangeFailed(inner);
throw inner;
}
} catch (IOException outer) {
conn.disconnect();
throw outer;
}
}