import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import java.io.File;
import java.io.FileOutputStream;
/**
* It's an example file which contains basic usage of CON's SMS API
* Other parameters from CON'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
*/
public class test_CON_API3 {
private static final String USERNAME = "maejima_test-u"; // need real username
private static final String PASSWORD = "password"; // need real password
private static final String URL = "https://www.******.jp/api/"; // URL to the service
public static void main(String args[]) {
send("test.jpg", "C:/Users/t-mae/files/");
}
private static void send(String fileName, String folder) {
CloseableHttpClient httpClient = HttpClientBuilder.create()
.build();
try {
String pathToTheFile = folder + fileName;
HttpEntity requestEntity = MultipartEntityBuilder.create()
.addTextBody("mobilenumber", "09018495712")
.addTextBody("smstext", "test {URL}")
.addBinaryBody("originalurl", new File(pathToTheFile), ContentType.create("image/jpeg"), pathToTheFile)
.build();
HttpPost post = new HttpPost(URL);
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(USERNAME, PASSWORD);
Header header = BasicScheme.authenticate(credentials, "UTF-8", false);
post.setHeader(header);
post.setEntity(requestEntity);
CloseableHttpResponse response = httpClient.execute(post);
HttpEntity httpEntity = response.getEntity();
if (null != httpEntity) {
File targetFile = new File(folder + "api3_result.csv");
FileOutputStream outstream = new FileOutputStream(targetFile);
httpEntity.writeTo(outstream);
}
EntityUtils.consume(httpEntity);
response.close();
httpClient.close();
} catch (Exception e) {
System.out.print(e);
} finally {
httpClient.getConnectionManager().shutdown();
}
}
}
その他の言語についてもお問い合わせください。