Java

Updated by lei_jiang

Sign

Arithmetic:md5(apiId + nonceId + apiKey)

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;

public class JavaDemo {

public static String md5(String data) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(data.getBytes());
byte[] digest = md.digest();
StringBuilder sb = new StringBuilder();
for (byte b : digest) {
sb.append(String.format("%02x", b));
}
return sb.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
}

public static void main(String[] args) {
String apiId = "xxxxx";
String apiKey = "xxxxxxx";
String nonceId = System.currentTimeMillis() + ":" + new Random().nextInt();
String authorization = md5(apiId + nonceId + apiKey);
System.out.println(authorization);
// fa8a8bdc6351ff8756856660b7b8dc3d
}
}

Creating a Cloud Phone Profile

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class JavaDemo {

public static String md5(String data) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(data.getBytes());
byte[] digest = md.digest();
StringBuilder sb = new StringBuilder();
for (byte b : digest) {
sb.append(String.format("%02x", b));
}
return sb.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
}

public static Map<String, String> getHeaders(String apiId, String apiKey) {
String nonceId = System.currentTimeMillis() + ":" + new Random().nextInt();
String authorization = md5(apiId + nonceId + apiKey);
Map<String, String> headers = new HashMap<>();
headers.put("Authorization", authorization);
headers.put("X-API-ID", apiId);
headers.put("X-NONCE-ID", nonceId);
return headers;
}

private static String getPayload(Map<String, Object> params) {
if (params == null) {
return null;
}
StringBuilder url = new StringBuilder();
for (String key : params.keySet()) {
url.append("\"").append(key).append("\"").append(":");
Object value = params.get(key);
if (value instanceof String) {
url.append("\"").append(value).append("\"");
} else {
url.append(value);
}
url.append(",");
}
if (url.length() > 0) {
return "{" + url.substring(0, url.length() - 1) + "}";
}
return "{}";
}

public static String doRequest(String apiId, String apiKey, String requestUrl, String method, Map<String, Object> params) throws IOException {
URL url = new URL(requestUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
try {
connection.setRequestMethod(method);
connection.setRequestProperty("Content-Type", "application/json");
Map<String, String> headers = getHeaders(apiId, apiKey);
for (String key : headers.keySet()) {
connection.setRequestProperty(key, headers.get(key));
}
if (params != null && !params.isEmpty()) {
connection.setDoOutput(true);
String postData = getPayload(params);
byte[] outputInBytes = postData.getBytes(StandardCharsets.UTF_8);
OutputStream os = connection.getOutputStream();
os.write(outputInBytes);
os.close();
}
int responseCode = connection.getResponseCode();
System.out.println("response code:" + responseCode);
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
StringBuilder response = new StringBuilder();
while ((line = reader.readLine()) != null) {
response.append(line);
}
reader.close();
return response.toString();
} finally {
connection.disconnect();
}
}

public static void main(String[] args) throws IOException {
String apiId = "xxx";
String apiKey = "xxxxx";
String host = "http://local.morelogin.com:54321";
// cloudphone create
Map<String, Object> params = new HashMap<>();
params.put("quantity", 2);
String response = doRequest(apiId, apiKey, host + "/openapi/cloudphone/create", "POST", params);
System.out.println("response:" + response);
}
}

CURL example:

curl -X POST http://local.morelogin.com:54321/openapi/cloudphone/create 
-H "Authorization: c4ec7d9c45dd72c4b43c2b3f66d7a764"
-H "X-NONCE-ID: 1711619019162:1874425727"
-H "X-API-ID: 1508955913945091"
-H "Content-Type: application/json"
-d "{""size"":2,""proxyId"":123}"

Successful response to the request

curl -X POST http://local.morelogin.com:54321/openapi/cloudphone/create 
-H "Authorization: c4ec7d9c45dd72c4b43c2b3f66d7a764"
-H "X-NONCE-ID: 1711619019162:1874425727"
-H "X-API-ID: 1508955913945091"
-H "Content-Type: application/json"
-d "{""size"":2,""proxyId"":123}"

Request Failure Response

response code:200
response:{
"code":19063,
"msg":"The number of profiles you have created has reached the limit",
"data":"0",
"requestId":"6895dec222224d6aaa41fbc58e123bfe"
}

Uploading files

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class JavaDemo {

public static String md5(String data) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(data.getBytes());
byte[] digest = md.digest();
StringBuilder sb = new StringBuilder();
for (byte b : digest) {
sb.append(String.format("%02x", b));
}
return sb.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
}

public static Map<String, String> getHeaders(String apiId, String apiKey) {
String nonceId = System.currentTimeMillis() + ":" + new Random().nextInt();
String authorization = md5(apiId + nonceId + apiKey);
Map<String, String> headers = new HashMap<>();
headers.put("Authorization", authorization);
headers.put("X-API-ID", apiId);
headers.put("X-NONCE-ID", nonceId);
return headers;
}

public static void main(String[] args) throws IOException {
String apiId = "xxx";
String apiKey = "xxxxx";
String host = "http://local.morelogin.com:54321";
// uploadFile
String uploadUrl = host + "/openapi/cloudphone/uploadFile";
String filePath = "xxx\\Desktop\\test.txt";
Map<String, Object> params = new HashMap<>();
params.put("id", 1);
params.put("downloadUrl", "https://s3.xx");
params.put("uploadDest", "/xxx");
uploadFileMultipart(apiId, apiKey, uploadUrl, filePath, params);
}

public static String uploadFileMultipart(String apiId, String apiKey, String uploadUrl, String filePath, Map<String, Object> params) throws IOException {
URL url = new URL(uploadUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
try {
connection.setRequestMethod("POST");
connection.setDoOutput(true);
Map<String, String> headers = getHeaders(apiId, apiKey);
for (String key : headers.keySet()) {
connection.setRequestProperty(key, headers.get(key));
}
String boundary = System.currentTimeMillis() + "";
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=----" + boundary);
OutputStream outputStream = connection.getOutputStream();
PrintWriter writer = new PrintWriter(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8), true);
File file = new File(filePath);
FileInputStream fileInputStream = new FileInputStream(file);
writer.append("------" + boundary).append(System.lineSeparator());
writer.append("Content-Disposition: form-data; name=\"file\"; filename=\"" + file.getName() + "\"").append(System.lineSeparator());
writer.append("Content-Type: " + URLConnection.guessContentTypeFromName(file.getName())).append(System.lineSeparator());
writer.append(System.lineSeparator()).flush();
byte[] buffer = new byte[1024];
int bytesRead = -1;
while ((bytesRead = fileInputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.flush();
fileInputStream.close();
writer.append(System.lineSeparator()).flush();
if (params != null && !params.isEmpty()) {
for (String key : params.keySet()) {
Object value = params.get(key);
writer.append("------" + boundary).append(System.lineSeparator());
writer.append("Content-Disposition: form-data; name=\"" + key + "\"").append(System.lineSeparator());
writer.append(System.lineSeparator()).append(value + "").append(System.lineSeparator());
}
}
writer.append("------" + boundary + "--").append(System.lineSeparator());
writer.flush();
writer.close();
outputStream.close();
int responseCode = connection.getResponseCode();
System.out.println("uploadFile Response Code: " + responseCode);
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
StringBuilder response = new StringBuilder();
while ((line = reader.readLine()) != null) {
response.append(line);
}
reader.close();
System.out.println("uploadFile Response: " + response);
return response.toString();
} finally {
// Close connection
connection.disconnect();
}
}
}


How did we do?