import javax.xml.bind.DatatypeConverter;
import java.io.DataOutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
public class test_con_api_1 {
private static final String GET = "GET";
private static final String POST = "POST";
private static final String USERNAME = "username"; // need real username
private static final String PASSWORD = "password"; // need real password
private static final String CON_URL = "http://www.******.jp/api/";
public static void main(String args[]) {
try {
final String queryStr = buildQuery("07000000002", "SDP_SUCCESS");
int codePost = sendPost(CON_URL, queryStr);
int codeGet = sendGet(CON_URL, queryStr);
} catch (Exception e) {
System.out.println(e);
}
}
private static int sendGet(String url, String query) throws Exception {
URL obj = new URL(url + "?" + query);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
makeAuth(con);
con.setRequestMethod(GET);
return outputResponseData(GET, con);
}
private static int 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();
return outputResponseData(POST, con);
}
private static String buildQuery(String mobile, String text) throws Exception {
StringBuilder builder = new StringBuilder();
builder.append("mobilenumber=").append(mobile);
builder.append("&smstext=").append(URLEncoder.encode(text, "UTF-8"));
String result = builder.toString();
System.out.println("Query: " + result);
return result;
}
private static void makeAuth(HttpURLConnection con) throws Exception {
String authStr = USERNAME + ":" + PASSWORD;
String authEncoded = DatatypeConverter.printBase64Binary(authStr.getBytes());
con.setRequestProperty("Authorization", "Basic " + authEncoded);
}
private static int outputResponseData(String method, HttpURLConnection con) throws Exception {
int responseCode = con.getResponseCode();
con.disconnect();
return responseCode;
}
}
その他の言語についてもお問い合わせください。