Created
September 4, 2018 07:11
-
-
Save shashankmehra/905107a5950f19091a69e4f94555a189 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
import java.net.URL; | |
import java.net.HttpURLConnection; | |
import java.io.*; | |
import org.json.JSONObject; | |
import java.util.Base64; | |
import java.nio.charset.StandardCharsets; | |
class KarixMessageExample { | |
public static void main(String[] args) { | |
try { | |
URL url = new URL ("https://api.karix.io/message/"); | |
HttpURLConnection con = (HttpURLConnection) url.openConnection(); | |
con.setDoOutput(true); | |
con.setDoInput(true); | |
con.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); | |
con.setRequestMethod("POST"); | |
String auth_id = "<Your Auth ID from dashboard>"; | |
String auth_token = "<Your Auth Token from dashboard>"; | |
String encoded = Base64.getEncoder().encodeToString( | |
(auth_id+":"+auth_token).getBytes(StandardCharsets.UTF_8)); | |
con.setRequestProperty("Authorization", "Basic "+encoded); | |
JSONObject message_request = new JSONObject(); | |
message_request.put("source","<your sender ID>"); | |
// This will give error | |
// message_request.put("destination","<E.164 destination>"); | |
message_request.put("destination",new String[] {"<E.164 destination>"}); | |
message_request.put("text","salut"); | |
OutputStream os = con.getOutputStream(); | |
os.write(message_request.toString().getBytes("UTF-8")); | |
os.close(); | |
StringBuilder sb = new StringBuilder(); | |
int result = con.getResponseCode(); | |
if (result == HttpURLConnection.HTTP_ACCEPTED) { | |
BufferedReader br = new BufferedReader( | |
new InputStreamReader(con.getInputStream(), "utf-8")); | |
String line = null; | |
while ((line = br.readLine()) != null) { | |
sb.append(line + "\n"); | |
} | |
br.close(); | |
System.out.println("Success: " + sb.toString()); | |
} else { | |
BufferedReader br = new BufferedReader( | |
new InputStreamReader(con.getErrorStream(), "utf-8")); | |
String line = null; | |
while ((line = br.readLine()) != null) { | |
sb.append(line + "\n"); | |
} | |
br.close(); | |
System.out.println(result); | |
System.out.println("Failure: " + sb.toString()); | |
} | |
} catch(Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment