小i机器人接口

string realm = "xiaoi.com";
string method = "POST";
string uri = "/robot/ask.do";
string key = "你的key";
string secret = "你的secret ;
byte[] b = new byte[40];
new Random().NextBytes(b);
char[] c = "abcdefghijklmnopqrstuvwxyz0123456789".ToArray();
string nonce = "";
Random r = new Random();
for (int i = 0; i < 40; i++)
{
nonce += c[r.Next(0, c.Length - 1)];
}
string ha1 = Common.SHA.SHA1(key + ":" + realm + ":" + secret);
string ha2 = Common.SHA.SHA1(method + ":" + uri);
string sign = Common.SHA.SHA1(ha1.ToLower() + ":" + nonce + ":" + ha2.ToLower());
HttpWebRequest httpRequest;
httpRequest = (HttpWebRequest)WebRequest.Create("http://nlp.xiaoi.com/robot/ask.do");
httpRequest.Headers["X-Auth"] = "app_key=" + '"' + key + '"' + ", nonce=" + '"' + nonce + '"' + ", signature=" + '"' + sign.ToLower() + '"';
httpRequest.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
returnStr = urlRequest.HttpPost(httpRequest, "question=" + HttpContext.Current.Server.UrlEncode(Content) + "&userId=&type=0", Encoding.UTF8);

/// <summary>
/// SHA1加密字符串
/// </summary>
/// <param name="source">源字符串</param>
/// <returns>加密后的字符串</returns>
public static string SHA1(string source)
{
byte[] value = Encoding.UTF8.GetBytes(source);
SHA1 sha = new SHA1CryptoServiceProvider();
byte[] result = sha.ComputeHash(value);

string delimitedHexHash = BitConverter.ToString(result);
string hexHash = delimitedHexHash.Replace("-", "");

return hexHash;
}

原文地址:https://www.cnblogs.com/Lance-Lan/p/3186857.html