Created
March 29, 2015 15:32
-
-
Save YukiMatsumura/50233e57814da3600f22 to your computer and use it in GitHub Desktop.
Deprecated HTTP Classes on Android5.1 ref: http://qiita.com/Yuki_312/items/85450138b85513bf1c00
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void disableConnectionReuseIfNecessary() { | |
// HTTP connection reuse which was buggy pre-froyo | |
if (Integer.parseInt(Build.VERSION.SDK) < Build.VERSION_CODES.FROYO) { | |
System.setProperty("http.keepAlive", "false"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
URL url = new URL("ftp://mirror.csclub.uwaterloo.ca/index.html"); | |
URLConnection urlConnection = url.openConnection(); | |
InputStream in = new BufferedInputStream(urlConnection.getInputStream()); | |
try { | |
readStream(in); | |
finally { | |
in.close(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
KeyStore keyStore = ...; | |
String algorithm = TrustManagerFactory.getDefaultAlgorithm(); | |
TrustManagerFactory tmf = TrustManagerFactory.getInstance(algorithm); | |
tmf.init(keyStore); | |
SSLContext context = SSLContext.getInstance("TLS"); | |
context.init(null, tmf.getTrustManagers(), null); | |
URL url = new URL("https://www.example.com/"); | |
HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection(); | |
urlConnection.setSSLSocketFactory(context.getSocketFactory()); | |
InputStream in = urlConnection.getInputStream(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
KeyStore keyStore = ...; | |
String algorithm = KeyManagerFactory.getDefaultAlgorithm(); | |
KeyManagerFactory kmf = KeyManagerFactory.getInstance(algorithm); | |
kmf.init(keyStore); | |
SSLContext context = SSLContext.getInstance("TLS"); | |
context.init(kmf.getKeyManagers(), null, null); | |
URL url = new URL("https://www.example.com/"); | |
HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection(); | |
urlConnection.setSSLSocketFactory(context.getSocketFactory()); | |
InputStream in = urlConnection.getInputStream(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
URL url = new URL("http://www.android.com/"); | |
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); | |
try { | |
InputStream in = new BufferedInputStream(urlConnection.getInputStream()); | |
readStream(in); | |
finally { | |
urlConnection.disconnect(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); | |
try { | |
urlConnection.setDoOutput(true); | |
urlConnection.setChunkedStreamingMode(0); | |
OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream()); | |
writeStream(out); | |
InputStream in = new BufferedInputStream(urlConnection.getInputStream()); | |
readStream(in); | |
finally { | |
urlConnection.disconnect(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
urlConnection.setRequestProperty("Accept-Encoding", "identity"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); | |
try { | |
InputStream in = new BufferedInputStream(urlConnection.getInputStream()); | |
if (!url.getHost().equals(urlConnection.getURL().getHost())) { | |
// we were redirected! Kick the user out to the browser to sign on? | |
... | |
} finally { | |
urlConnection.disconnect(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Authenticator.setDefault(new Authenticator() { | |
protected PasswordAuthentication getPasswordAuthentication() { | |
return new PasswordAuthentication(username, password.toCharArray()); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CookieManager cookieManager = new CookieManager(); | |
CookieHandler.setDefault(cookieManager); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
HttpCookie cookie = new HttpCookie("lang", "fr"); | |
cookie.setDomain("twitter.com"); | |
cookie.setPath("/"); | |
cookie.setVersion(0); | |
cookieManager.getCookieStore().add(new URI("http://twitter.com/"), cookie); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void disableConnectionReuseIfNecessary() { | |
// Work around pre-Froyo bugs in HTTP connection reuse. | |
if (Integer.parseInt(Build.VERSION.SDK) < Build.VERSION_CODES.FROYO) { | |
System.setProperty("http.keepAlive", "false"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment