java api接口调用 java实现小i机器人api接口调用示例(java面试题)

网友投稿 490 2022-06-06


想了解java实现小i机器人api接口调用示例的相关内容吗,在本文为您仔细讲解java api接口调用的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:java,小i机器人,api接口调用,下面大家一起来学习吧。
复制代码 代码如下:

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;
 }
}



版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:window所有进程 window调用api列出当前所有进程示例(windows进程管理)
下一篇:2048游戏 win32 api实现2048游戏示例(2048游戏下载官方正版)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~