程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> java完成小i機械人api接口挪用示例

java完成小i機械人api接口挪用示例

編輯:關於JAVA

java完成小i機械人api接口挪用示例。本站提示廣大學習愛好者:(java完成小i機械人api接口挪用示例)文章只能為提供參考,不一定能成為您想要的結果。以下是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;
 }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved