Skip to content

Commit

Permalink
Moved setupUrlConnection() to the utils library
Browse files Browse the repository at this point in the history
  • Loading branch information
aforcier committed Dec 17, 2015
1 parent aa8a1d3 commit 261e48b
Showing 1 changed file with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.wordpress.android.util;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;

public class HTTPUtils {
public static final int REQUEST_TIMEOUT_MS = 30000;

/**
* Builds an HttpURLConnection from a URL and header map. Will force HTTPS usage if given an Authorization header.
* @throws IOException
*/
public static HttpURLConnection setupUrlConnection(String url, Map<String, String> headers) throws IOException {
// Force HTTPS usage if an authorization header was specified
if (headers.keySet().contains("Authorization")) {
url = UrlUtils.makeHttps(url);
}

HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
conn.setReadTimeout(REQUEST_TIMEOUT_MS);
conn.setConnectTimeout(REQUEST_TIMEOUT_MS);

for (Map.Entry<String, String> entry : headers.entrySet()) {
conn.setRequestProperty(entry.getKey(), entry.getValue());
}

return conn;
}
}

0 comments on commit 261e48b

Please sign in to comment.