Created
April 12, 2017 02:00
-
-
Save AaronVigal/31831e54a08a979a50e7365781904cb2 to your computer and use it in GitHub Desktop.
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
package com.aaronvigal.reconclient; | |
import android.os.StrictMode; | |
import java.io.BufferedReader; | |
import java.io.DataOutputStream; | |
import java.io.InputStreamReader; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
//import org.apache.commons.lang3.StringEscapeUtils.escapeHtml4; | |
public class PostRequest { | |
public String makeRequest() { | |
try { | |
// Disable Strict Mode | |
if (android.os.Build.VERSION.SDK_INT > 9) { | |
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); | |
StrictMode.setThreadPolicy(policy); | |
} | |
HttpURLConnection con = (HttpURLConnection) new URL("http://requestb.in/opmp57op").openConnection(); | |
con.setRequestMethod("POST"); | |
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); | |
// Base 64 encode image | |
// String b64 = escapeHtml4(Base64.encode(FileUtils.readFileToByteArray())); | |
// https://stackoverflow.com/questions/1265282/recommended-method-for-escaping-html-in-java | |
// https://stackoverflow.com/questions/30259141/how-to-add-apache-commons-collections-in-android-studio-gradle/ | |
// https://stackoverflow.com/questions/10226046/java-convert-image-to-base64 | |
// https://stackoverflow.com/questions/16439587/android-os-networkonmainthreadexception-with-android-4-2 | |
// https://stackoverflow.com/questions/32858061/android-send-https-post-request-to-the-server-without-deprecated-metods | |
DataOutputStream wr = new DataOutputStream(con.getOutputStream()); | |
wr.writeBytes("b64=blahblahblah&g="); | |
wr.flush(); | |
wr.close(); | |
System.out.println("----------------------------------------------------------------"); | |
System.out.println("\nSending 'POST' request to URL: " + "https://requestb.in/111ijno1"); | |
System.out.println("Post parameters : " + "b64=blahblahblah&g="); | |
System.out.println("Response Code : " + con.getResponseCode()); | |
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); | |
String inputLine; | |
StringBuilder response = new StringBuilder(); | |
while ((inputLine = in.readLine()) != null) { | |
response.append(inputLine); | |
} | |
in.close(); | |
System.out.println(response.toString()); | |
System.out.println("----------------------------------------------------------------"); | |
return response.toString(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return ""; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment