private Map<String, String> parseHeaders(HttpURLConnection conn) { Map<String, String> headers = new HashMap<String, String>(); for (String key : conn.getHeaderFields().keySet()) { headers.put(key, conn.getHeaderFields().get(key).get(0)); } return headers; }
Response(HttpURLConnection connection) throws IOException { try { connection.connect(); code = connection.getResponseCode(); headers = parseHeaders(connection); stream = isSuccessful() ? connection.getInputStream() : connection.getErrorStream(); } catch (UnknownHostException e) { throw new OAuthException("The IP address of a host could not be determined.", e); } }
void addBody(HttpURLConnection conn, byte[] content) throws IOException { conn.setRequestProperty(CONTENT_LENGTH, String.valueOf(content.length)); // Set default content type if none is set. if (conn.getRequestProperty(CONTENT_TYPE) == null) { conn.setRequestProperty(CONTENT_TYPE, DEFAULT_CONTENT_TYPE); } conn.setDoOutput(true); conn.getOutputStream().write(content); }
Response doSend() throws IOException { connection.setRequestMethod(this.verb.name()); if (connectTimeout != null) { connection.setConnectTimeout(connectTimeout.intValue()); } if (readTimeout != null) { connection.setReadTimeout(readTimeout.intValue()); } if (boundary != null) { connection.setRequestProperty(CONTENT_TYPE, "multipart/form-data; boundary=" + boundary); } addHeaders(connection); if (verb.equals(Verb.PUT) || verb.equals(Verb.POST)) { addBody(connection, getByteBodyContents()); } return new Response(connection); }
void addHeaders(HttpURLConnection conn) { for (String key : headers.keySet()) conn.setRequestProperty(key, headers.get(key)); }