复制代码代码如下:
com.weixin.util 包;
导入java.io.IOException;
导入 java.util.Random;
导入 org.apache.commons.codec.binary.Hex;
导入 org.apache.commons.codec.digest.DigestUtils;
导入 org.apache.commons.httpclient.HttpClient;
导入 org.apache.commons.httpclient.HttpException;
导入 org.apache.commons.httpclient.methods.PostMethod;
导入 org.apache.commons.httpclient.params.HttpMethodParams;
导入 org.apache.commons.lang.StringUtils;
公开课小我{
私有最终静态字符串 APP_KEY = "*****";
私有最终静态字符串 APP_SECRET = "****";
公共字符串苔丝(字符串键,字符串名称){
String Realm = "xiaoi.com";
字符串方法=“POST”;
字符串 uri = "/robot/ask.do";
字节 [] b = 新字节 [20];
new Random().nextBytes(b);
String nonce = new String(Hex.encodeHex(b));
String HA1 = DigestUtils.shaHex(StringUtils.join(new String[] {
APP_KEY, 领域, APP_SECRET }, ":"));
String HA2 = DigestUtils.shaHex(StringUtils.join(new String[] { 方法,
uri }, ":"));
字符串符号 = DigestUtils.shaHex(StringUtils.join(new String[] { HA1,
随机数,HA2},“:”));
字符串 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=/""
+ 随机数 + "/", 签名=/"" + 符号 + "/"");
pm.setParameter("平台", "微信");
pm.setParameter("类型", "0");
pm.setParameter("userId", 名称);
pm.setParameter("问题", key);
int 重新编码;
尝试 {
re_code = hc.executeMethod(pm);
如果(re_code==200){
str = pm.getResponseBodyAsString();
}
} catch (HttpException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
返回字符串;
}
}