赞
踩
- import cn.hutool.core.util.RandomUtil;
- import cn.hutool.http.HttpRequest;
- import org.springframework.util.DigestUtils;
-
- import java.net.URLEncoder;
- import java.util.*;
-
- class Test1 {
-
- final static String APP_ID = "";//腾讯云AI的 的appid
- final static String APP_KEY = "";//腾讯云AI的 APP_KEY
-
- public static void main(String[] args) {
- Map<String,Object> map = new HashMap();
- map.put("app_id",APP_ID);
- map.put("time_stamp",String.valueOf(new Date().getTime()/1000));
- map.put("nonce_str", RandomUtil.simpleUUID());
- map.put("session",RandomUtil.randomNumbers(5));
- map.put("question","你是人工智障吗");
- String siges = formatUrlParam(map, "utf-8", false)+"&app_key="+APP_KEY;
- map.put("sign", DigestUtils.md5DigestAsHex(siges.getBytes()).toUpperCase());
- HttpRequest result2 = HttpRequest.post("https://api.ai.qq.com/fcgi-bin/nlp/nlp_textchat");
- result2.form(map);
- System.out.println(result2.execute().body());
-
- }
-
- //拼接url
- public static String formatUrlParam(Map<String, Object> param, String encode, boolean isLower) {
- String params = "";
- Map<String, Object> map = param;
-
- try {
- List<Map.Entry<String, String>> itmes = new ArrayList(map.entrySet());
- //对所有传入的参数按照字段名从小到大排序
- //Collections.sort(items); 默认正序
- //可通过实现Comparator接口的compare方法来完成自定义排序
- Collections.sort(itmes, new Comparator<Map.Entry<String, String>>() {
- @Override
- public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2) {
- // TODO Auto-generated method stub
- return (o1.getKey().toString().compareTo(o2.getKey()));
- }
- });
- //构造URL 键值对的形式
- StringBuffer sb = new StringBuffer();
- for (Map.Entry<String, String> item : itmes) {
- if (item.getKey() != null && item.getKey()!="") {
- String key = item.getKey();
- String val = item.getValue();
- val = URLEncoder.encode(val, encode);
- if (isLower) {
- sb.append(key.toLowerCase() + "=" + val);
- } else {
- sb.append(key + "=" + val);
- }
- sb.append("&");
- }
- }
-
- params = sb.toString();
- if (!params.isEmpty()) {
- params = params.substring(0, params.length() - 1);
- }
- } catch (Exception e) {
- return "";
- }
- return params;
- }
-
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。