测试
java web
package com.tukrin.ocean; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.HashMap; import java.util.Map; import com.alibaba.fastjson.JSONObject; /** * 欧神人脸平台工具类 * @author aisi * */ public class OceanUtil { public static final String OCEAN_TOKEN_URL = "http://192.168.1.221:10006/ocean/auth/token"; /** * 获得token * @param appId * @param appSecret * @return String */ public static String getOceanToken(String appId, String appSecret){ if(appId == null && appSecret == null){ appId = "a54fce347adf4ca88f740c0eb230ec31"; appSecret = "$2a$10$dJDxml5rWVC4EanD7TbsgOfqTwcr/tcq5DAGR6KIBrwqo4GuPUHte"; } HttpURLConnection connection = null; BufferedReader reader = null; String token = null; try { URL url = new URL(OCEAN_TOKEN_URL); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setDoOutput(true); connection.setRequestProperty("Content-Type", "application/json"); connection.setConnectTimeout(8000); connection.setReadTimeout(6000); //请求参数 Map<String, Object> map = new HashMap<String, Object>(); map.put("appId", appId); map.put("appSecret", appSecret); DataOutputStream out = new DataOutputStream(connection.getOutputStream()); out.writeBytes(JSONObject.toJSONString(map)); //读取返回数据 InputStream in = connection.getInputStream(); reader = new BufferedReader(new InputStreamReader(in)); StringBuilder sb = new StringBuilder(); String line = null; while((line = reader.readLine()) != null){ sb.append(new String(line.getBytes(), "UTF-8")); } //获取token JSONObject jsonObject1 = JSONObject.parseObject(sb.toString()); JSONObject jsonObject2 = (JSONObject) jsonObject1.get("data"); token = (String) jsonObject2.get("token"); } catch (Exception e) { e.printStackTrace(); } return token; } }