国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

java實現小i機器人api接口調用示例

2019-11-26 15:31:42
字體:
來源:轉載
供稿:網友

復制代碼 代碼如下:

package com.weixin.util;

import java.io.IOException;
import java.util.Random;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.lang.StringUtils;

public class XiaoI {
 private final static String APP_KEY = "*****";
 private final static String APP_SECRET = "****";

 public String Tess(String key, String names) {
  String realm = "xiaoi.com";
  String method = "POST";
  String uri = "/robot/ask.do";
  byte[] b = new byte[20];
  new Random().nextBytes(b);
  String nonce = new String(Hex.encodeHex(b));
  String HA1 = DigestUtils.shaHex(StringUtils.join(new String[] {
    APP_KEY, realm, APP_SECRET }, ":"));
  String HA2 = DigestUtils.shaHex(StringUtils.join(new String[] { method,
    uri }, ":"));
  String sign = DigestUtils.shaHex(StringUtils.join(new String[] { HA1,
    nonce, HA2 }, ":"));

  String str = null;

  HttpClient hc = new HttpClient();
  PostMethod pm = new PostMethod("http://nlp.xiaoi.com/robot/ask.do");
  pm.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,
    "utf-8");
  pm.addRequestHeader("X-Auth", "app_key=/"7HHk65oE2Ngy/", nonce=/""
    + nonce + "/", signature=/"" + sign + "/"");
  pm.setParameter("platform", "weixin");
  pm.setParameter("type", "0");
  pm.setParameter("userId", names);
  pm.setParameter("question", key);
  int re_code;
  try {
   re_code = hc.executeMethod(pm);
   if (re_code == 200) {
    str = pm.getResponseBodyAsString();
   }
  } catch (HttpException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return str;
 }
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 台江县| 合江县| 军事| 张掖市| 汝城县| 白玉县| 迭部县| 石狮市| 堆龙德庆县| 渝中区| 濮阳市| 喀什市| 时尚| 盖州市| 江达县| 海丰县| 鹰潭市| 高邮市| 淮滨县| 嘉善县| 阿拉善右旗| 湖北省| 通江县| 卓资县| 丹巴县| 兴义市| 喀喇| 许昌县| 闽清县| 青川县| 长岛县| 义马市| 杭锦旗| 时尚| 吴川市| 抚顺市| 师宗县| 疏勒县| 通渭县| 嘉峪关市| 阿勒泰市|