复制代码代码如下:
paket com.weixin.util;
impor java.io.IOException;
import java.util.Acak;
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;
kelas publik XiaoI {
String statis akhir pribadi APP_KEY = "*****";
String statis akhir pribadi APP_SECRET = "****";
public String Tess(Kunci string, Nama string) {
String ranah = "xiaoi.com";
Metode string = "POSTING";
String uri = "/robot/ask.do";
byte[] b = byte baru[20];
Acak baru().nextBytes(b);
String nonce = String baru(Hex.encodeHex(b));
String HA1 = DigestUtils.shaHex(StringUtils.join(String baru[] {
APP_KEY, ranah, APP_SECRET }, ":"));
String HA2 = DigestUtils.shaHex(StringUtils.join(String baru[] { metode,
uri }, ":"));
Tanda string = DigestUtils.shaHex(StringUtils.join(String baru[] { HA1,
tidak, HA2 }, ":"));
String str = nol;
HttpClient hc = HttpClient() baru;
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 + "/", tanda tangan=/"" + tanda + "/"");
pm.setParameter("platform", "weixin");
pm.setParameter("ketik", "0");
pm.setParameter("userId", nama);
pm.setParameter("pertanyaan", kunci);
int kode ulang;
mencoba {
re_code = hc.executeMethod(pm);
jika (kode_ulang == 200) {
str = pm.getResponseBodyAsString();
}
} tangkapan (HttpException e) {
// TODO Blok tangkapan yang dibuat secara otomatis
e.printStackTrace();
} tangkapan (IOException e) {
// TODO Blok tangkapan yang dibuat secara otomatis
e.printStackTrace();
}
kembalikan str;
}
}