/**
* It's an example file which contains basic usage of OPE's SMS API
* Other parameters from OPE's API specification can be used according to business logic and contract notes
* This file must NOT be used for production services as is
* Production version of API implementation must handle different error situations and http error codes
*/
import javax.xml.bind.DatatypeConverter;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class test_OPE_API1 {
private static final String GET = "GET";
private static final String POST = "POST";
private static final String USERNAME = ""; // need real username
private static final String PASSWORD = ""; // need real password
private static final String OPE_URL = "https://www.**********/sms/api/";
public static void main(String args[]) {
try {
sendPost(OPE_URL, buildQuery());
} catch (Exception e) {
System.out.println(e);
}
}
private static void sendGet(String url, String query) throws Exception {
URL obj = new URL(url + "?" + query);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
makeAuth(con);
con.setRequestMethod(GET);
outputResponseData(GET, con);
}
private static void sendPost(String url, String query) throws Exception {
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
makeAuth(con);
con.setRequestMethod(POST);
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(query);
wr.flush();
wr.close();
outputResponseData(POST, con);
}
private static String buildQuery() {
// method 4 return "mobilecareer=b&mobilenumber=080********&method=4&ipphone=050********&holdingtime=1&authcode=1234&authtime=3&waitauth=5";
// method 5 return "mobilecareer=b&mobilenumber=080********&method=5&ipphone=050********&holdingtime=1&repeatcall=1&1stcall=1&intervalcall=1&ringingtime=15&authcode=1234&authtime=3&waitauth=5";
// method 6 return "mobilecareer=b&mobilenumber=080********&method=6&ipphone=050********&holdingtime=10&authcode=1234&authtime=3&waitauth=5&authcodewait=1";
// method 7 return "mobilecareer=b&mobilenumber=080********&method=7&ipphone=050********&holdingtime=1&repeatcall=1&1stcall=1&intervalcall=1&ringingtime=15&authcode=1234&authtime=2&waitauth=5";
return "mobilenumber=080********&smstext=test";
}
private static void makeAuth(HttpURLConnection con) {
String authStr = USERNAME + ":" + PASSWORD;
String authEncoded = DatatypeConverter.printBase64Binary(authStr.getBytes());
con.setRequestProperty("Authorization", "Basic " + authEncoded);
}
private static void outputResponseData(String method, HttpURLConnection con) throws Exception {
int responseCode = con.getResponseCode();
System.out.println("Response code: " + responseCode);
System.out.println("Response body: ");
InputStream is = responseCode == 200 ? con.getInputStream() : con.getErrorStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String strCurrentLine;
while ((strCurrentLine = br.readLine()) != null) {
System.out.println(strCurrentLine);
}
br.close();
con.disconnect();
}
}
その他の言語についてもお問い合わせください。