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;
}
}
相关文章
聊聊Spring循环依赖三级缓存是否可以减少为二级缓存的情况
这篇文章主要介绍了聊聊Spring循环依赖三级缓存是否可以减少为二级缓存的情况,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2021-02-02java.lang.UnsatisfiedLinkError: %1 不是有效的Win32应用程序错误解决
这篇文章主要给大家介绍了关于java.lang.UnsatisfiedLinkError: %1 不是有效的Win32应用程序错误的解决方法,文中介绍的非常详细,需要的朋友们可以参考学习,下面来一起看看吧。2017-03-03java环境变量为什么要配置path和classpath详细解答
为何配置path?为何配置classpath?当时初学java时只是关心如何做而不去关心这些问题,接下来介绍一下,感兴趣的朋友可以参考下哦2013-01-01
最新评论