Last active
July 12, 2018 03:09
-
-
Save mplacona/08527ab6ee9043bea41c to your computer and use it in GitHub Desktop.
Android HTTP Helper
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
/* | |
* Copyright (c) 2011 by Twilio, Inc., all rights reserved. | |
* | |
* Use of this software is subject to the terms and conditions of | |
* the Twilio Terms of Service located at http://www.twilio.com/legal/tos | |
*/ | |
package com.twilio.ipmessaging.util; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.io.Reader; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
public abstract class HttpHelper | |
{ | |
private static String stringFromInputStream(InputStream is) throws IOException | |
{ | |
char[] buf = new char[1024]; | |
StringBuilder out = new StringBuilder(); | |
Reader in = new InputStreamReader(is, "UTF-8"); | |
int bin; | |
while ((bin = in.read(buf, 0, buf.length)) >= 0) { | |
out.append(buf, 0, bin); | |
} | |
return out.toString(); | |
} | |
public static String httpGet(String url) throws Exception { | |
URL urlObj = new URL(url); | |
HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection(); | |
conn.setConnectTimeout(45000); | |
conn.setReadTimeout(30000); | |
conn.setDoInput(true); | |
int responseCode = conn.getResponseCode(); | |
if (responseCode == HttpURLConnection.HTTP_OK) { | |
InputStream is = conn.getInputStream(); | |
String capabilityToken = stringFromInputStream(is); | |
is.close(); | |
conn.disconnect(); | |
return capabilityToken; | |
} else { | |
conn.disconnect(); | |
throw new Exception("Got error code " + responseCode | |
+ " from server"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment