import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Paths;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class HttpClient {
public static final String POST_URL = "http://127.0.0.1:3000/identity/liveness_image_verification/stateless";
private static AESCipher cipher = new AESCipher("dcbbad6765e14139a07d34b92292a672", "df25d188a061");
public static void Post() throws Exception {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost post = new HttpPost(POST_URL);
MultipartEntityBuilder entity = MultipartEntityBuilder.create();
String encryptedImage = cipher.encrypt(
Files.readAllBytes(Paths.get("src/main/resources/face_01.jpg")));
entity.addPart("liveness_file", new FileBody(new File("src/main/resources/mobile.protobuf")));
entity.addPart("encrypted_image", new StringBody(encryptedImage));
entity.addPart("auto_rotate", new StringBody("true"));
post.setEntity(entity.build());
HttpResponse response = httpclient.execute(post);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity respEntity = response.getEntity();
BufferedReader reader = new BufferedReader(new InputStreamReader(respEntity.getContent()));
String line = reader.readLine();
System.out.println(line);
} else {
HttpEntity respEntity = response.getEntity();
String responseString = EntityUtils.toString(respEntity);
System.out.println("error:" + response.getStatusLine().getStatusCode()
+ " " + response.getStatusLine().getReasonPhrase());
System.out.println("cause of error:"+responseString);
}
}
public static void main(String[] args) throws Exception {
Post();
}
}