private ToJoson DoAPI(string Msg) { INFO = Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(Msg)); getURl = "http://www.tuling123.com/openapi/api?key=" + APIKEY + "&info=" + INFO; Uri uri = new Uri(getURl); HttpWebRequest getUrl = WebRequest.Create(uri) as HttpWebRequest; getUrl.Method = "GET"; HttpWebResponse response = getUrl.GetResponse() as HttpWebResponse; Stream respStream = response.GetResponseStream(); StreamReader stream = new StreamReader(respStream, Encoding.UTF8); string respStr = stream.ReadToEnd(); stream.Close(); JavaScriptSerializer js = new JavaScriptSerializer(); ToJoson joson = js.Deserialize <ToJoson>(respStr); code = joson.code; text = joson.text; url = joson.url; List <list> list = joson.list; return(joson); }
private void btn_send_Click(object sender, EventArgs e) { StringBuilder receiveMsg = new StringBuilder(); string msg = rtb_SendMsg.Text.Trim(); txtRecMsg.AppendText("我:" + DateTime.Now + Environment.NewLine); txtRecMsg.AppendText(msg + "\r\n"); ToJoson joson = DoAPI(msg); List <list> list = joson.list; switch (joson.code) { //文本类 case "100000": receiveMsg.Append(joson.text + Environment.NewLine); break; //列车 //网址类数据 case "200000": receiveMsg.Append(joson.text + Environment.NewLine) .Append(joson.url); break; //新闻 case "302000": receiveMsg.Append(joson.text + Environment.NewLine) .Append(joson.url); foreach (list listDetail in list) { receiveMsg.Append(listDetail.source + " " + listDetail.article + Environment.NewLine) .Append(listDetail.detailurl + Environment.NewLine); } break; //菜谱、视频、小说 case "308000": receiveMsg.Append(joson.text + Environment.NewLine) .Append(joson.url); foreach (list listDetail in list) { receiveMsg.Append(listDetail.name + " " + listDetail.info + Environment.NewLine) .Append(listDetail.detailurl + Environment.NewLine); } break; //key的长度错误(32位) case "40001": receiveMsg.Append("key的长度错误(32位)" + Environment.NewLine); break; //请求内容为空 case "40002": receiveMsg.Append("请求内容为空" + Environment.NewLine); break; //Key错误或帐号未激活 case "40003": receiveMsg.Append("Key错误或帐号未激活" + Environment.NewLine); break; //当天请求次数已用完 case "40004": receiveMsg.Append("当天请求次数已用完" + Environment.NewLine); break; //暂不支持该功能 case "40005": receiveMsg.Append("暂不支持该功能" + Environment.NewLine); break; //服务器升级中 case "40006": receiveMsg.Append("服务器升级中" + Environment.NewLine); break; //服务器数据格式异常 case "40007": receiveMsg.Append("服务器数据格式异常" + Environment.NewLine); break; default: break; } rtb_replyMSG.Text = receiveMsg.ToString(); txt_combine.Text = rtb_replyMSG.Text; txtRecMsg.AppendText("机器人:" + DateTime.Now + Environment.NewLine); txtRecMsg.AppendText(receiveMsg.ToString() + "\r\n"); txtRecMsg.Select(txtRecMsg.TextLength, 0); Clear(); }