Last active
August 29, 2015 14:06
-
-
Save shahzore-qureshi/7808bb3bca01ed000aae to your computer and use it in GitHub Desktop.
Basic Android HTTP Client
This file contains hidden or 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
| import java.io.BufferedReader; | |
| import java.io.FilterInputStream; | |
| import java.io.IOException; | |
| import java.io.InputStream; | |
| import java.io.InputStreamReader; | |
| import java.io.OutputStreamWriter; | |
| import java.net.HttpURLConnection; | |
| import java.net.URL; | |
| import java.util.zip.GZIPInputStream; | |
| import java.io.UnsupportedEncodingException; | |
| import java.net.URI; | |
| import java.net.URLDecoder; | |
| import java.net.CookieHandler; | |
| import java.net.CookieManager; | |
| import java.net.CookiePolicy; | |
| class Test | |
| { | |
| public static void main (String[] args) throws java.lang.Exception | |
| { | |
| String url = "google.com"; | |
| String test = HTTPMethodTasks.GetMethod(url, null); | |
| System.out.println(test); | |
| } | |
| } | |
| class PostHeader { | |
| public String headerValue; | |
| public String headerField; | |
| public PostHeader() { | |
| } | |
| public PostHeader(String value, String field) { | |
| this.headerValue = value; | |
| this.headerField = field; | |
| } | |
| } | |
| class HTTPMethodTasks { | |
| public static String GetMethod(String urlString, PostHeader[] postHeaders) | |
| throws IOException { | |
| URL url = convertToURLEscapingIllegalCharacters(urlString); | |
| CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL)); | |
| HttpURLConnection connection = (HttpURLConnection) url.openConnection(); | |
| connection.setRequestMethod("GET"); | |
| if (postHeaders != null) { | |
| for (int i = 0; i < postHeaders.length; i++) { | |
| String field = postHeaders[i].headerField; | |
| String value = postHeaders[i].headerValue; | |
| connection.addRequestProperty(field, value); | |
| } | |
| } | |
| connection.setInstanceFollowRedirects(false); | |
| connection.connect(); | |
| InputStream stream = connection.getInputStream(); | |
| String line = ""; | |
| InputStreamReader isr = new InputStreamReader(stream); | |
| BufferedReader reader = new BufferedReader(isr); | |
| StringBuilder sb = new StringBuilder(); | |
| while ((line = reader.readLine()) != null) { | |
| sb.append(line + "\n"); | |
| } | |
| isr.close(); | |
| reader.close(); | |
| return sb.toString(); | |
| } | |
| public static String PostMethod(String urlString, String urlParameters, | |
| PostHeader[] postHeaders) throws IOException { | |
| HttpURLConnection postConnection = null; | |
| OutputStreamWriter request = null; | |
| URL url = convertToURLEscapingIllegalCharacters(urlString); | |
| postConnection = (HttpURLConnection) url.openConnection(); | |
| postConnection.setRequestMethod("POST"); | |
| postConnection.setRequestProperty("Content-Language", "en-US"); | |
| postConnection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length)); | |
| postConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); | |
| if (postHeaders != null) { | |
| for (int i = 0; i < postHeaders.length; i++) { | |
| String field = postHeaders[i].headerField; | |
| String value = postHeaders[i].headerValue; | |
| postConnection.setRequestProperty(field, value); | |
| } | |
| } | |
| postConnection.setUseCaches(false); | |
| postConnection.setDoOutput(true); | |
| postConnection.setDoInput(true); | |
| request = new OutputStreamWriter(postConnection.getOutputStream()); | |
| if (urlParameters != null) { | |
| request.write(urlParameters); | |
| } | |
| request.flush(); | |
| String line = ""; | |
| InputStream stream = postConnection.getInputStream(); | |
| InputStreamReader isr = new InputStreamReader(stream); | |
| BufferedReader reader = new BufferedReader(isr); | |
| StringBuilder sb = new StringBuilder(); | |
| while ((line = reader.readLine()) != null) { | |
| sb.append(line + "\n"); | |
| } | |
| isr.close(); | |
| reader.close(); | |
| return sb.toString(); | |
| } | |
| public static URI convertToURIEscapingIllegalCharacters(String string){ | |
| try { | |
| String decodedURL = URLDecoder.decode(string, "UTF-8"); | |
| URL url = new URL(decodedURL); | |
| URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef()); | |
| return uri; | |
| } catch (Exception ex) { | |
| ex.printStackTrace(); | |
| return null; | |
| } | |
| } | |
| public static URL convertToURLEscapingIllegalCharacters(String string){ | |
| try { | |
| URI uri = convertToURIEscapingIllegalCharacters(string); | |
| return uri.toURL(); | |
| } catch (Exception ex) { | |
| ex.printStackTrace(); | |
| return null; | |
| } | |
| } | |
| public static String escapeIllegalCharacters(String string){ | |
| String decodedURL = null; | |
| try { | |
| decodedURL = URLDecoder.decode(string, "UTF-8"); | |
| } catch (UnsupportedEncodingException e) { | |
| e.printStackTrace(); | |
| } | |
| return decodedURL; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment