Copy Code code as follows:
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;
}
}