-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathSendSMS.java
53 lines (46 loc) · 1.92 KB
/
SendSMS.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import java.net.URLEncoder;
import java.net.URL;
import java.net.URLConnection;
import java.io.OutputStreamWriter;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import javax.xml.bind.DatatypeConverter;
class SendSMS
{
static void sendSMS () {
try {
// Construct POST data
String data = URLEncoder.encode("from", "UTF-8") + "=" + URLEncoder.encode("JavaElk", "UTF-8");
data += "&" + URLEncoder.encode("to", "UTF-8") + "=" + URLEncoder.encode("+46705569900", "UTF-8");
data += "&" + URLEncoder.encode("message", "UTF-8") + "=" + URLEncoder.encode("Freshly brewed coffee is tasteful!", "UTF-8");
// Make HTTP POST request
URL url = new URL("https://api.46elks.com/a1/SMS");
URLConnection conn = url.openConnection();
String username = "u1234123412341234123412341234";
String password = "ABCD1234ABCD1234ABCD1234ABCD1234";
String base64string = DatatypeConverter.printBase64Binary((username + ":" + password).getBytes("UTF-8"));
String basicAuth = "Basic " + base64string;
conn.setRequestProperty("Authorization", basicAuth);
conn.setRequestProperty("Content-Length", Integer.toString(data.getBytes("UTF-8").length));
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
// Handle response data here (currently, just print out response)
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
System.out.println(line);
}
wr.close();
rd.close();
} catch (Exception e) {
}
}
public static void main(String args[])
{
System.out.println("Hello World!");
sendSMS();
}
}