复制代码代码如下:
pacote com.weixin.util;
importar java.io.IOException;
importar java.util.Random;
importar org.apache.commons.codec.binary.Hex;
importar org.apache.commons.codec.digest.DigestUtils;
importar org.apache.commons.httpclient.HttpClient;
importar org.apache.commons.httpclient.HttpException;
importar org.apache.commons.httpclient.methods.PostMethod;
importar org.apache.commons.httpclient.params.HttpMethodParams;
importar org.apache.commons.lang.StringUtils;
classe pública XiaoI {
string estática final privada APP_KEY = "*****";
string estática final privada APP_SECRET = "****";
public String Tess(chave de string, nomes de string) {
String reino = "xiaoi.com";
Método String = "POST";
String uri = "/robot/ask.do";
byte[] b = novo byte[20];
novo Random().nextBytes(b);
String nonce = new String(Hex.encodeHex(b));
String HA1 = DigestUtils.shaHex(StringUtils.join(new String[] {
APP_KEY, domínio, APP_SECRET }, ":"));
String HA2 = DigestUtils.shaHex(StringUtils.join(new String[] {método,
uri}, ":"));
Sinal de string = DigestUtils.shaHex(StringUtils.join(new String[] { HA1,
nonce, HA2 }, ":"));
String str = nulo;
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 + "/", assinatura=/"" + sinal + "/"");
pm.setParameter("plataforma", "weixin");
pm.setParameter("tipo", "0");
pm.setParameter("userId", nomes);
pm.setParameter("pergunta", chave);
int re_código;
tentar {
re_code = hc.executeMethod(pm);
if (re_código == 200) {
str = pm.getResponseBodyAsString();
}
} catch (HttpException e) {
// TODO Bloco catch gerado automaticamente
e.printStackTrace();
} catch (IOException e) {
// TODO Bloco catch gerado automaticamente
e.printStackTrace();
}
retornar string;
}
}