//post请求 //public static string HttpPost() //{ // string strURL = "http://localhost/WinformSubmit.php"; // System.Net.HttpWebRequest request; // request = (System.Net.HttpWebRequest)WebRequest.Create(strURL); // //Post请求方式 // request.Method = "POST"; // // 内容类型 // request.ContentType = "application/x-www-form-urlencoded"; // // 参数经过URL编码 // string paraUrlCoded = System.Web.HttpUtility.UrlEncode("keyword"); // paraUrlCoded += "=" + System.Web.HttpUtility.UrlEncode("多月"); // byte[] payload; // //将URL编码后的字符串转化为字节 // payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded); // //设置请求的 ContentLength // request.ContentLength = payload.Length; // //获得请 求流 // System.IO.Stream writer = request.GetRequestStream(); // //将请求参数写入流 // writer.Write(payload, 0, payload.Length); // // 关闭请求流 // writer.Close(); // System.Net.HttpWebResponse response; // // 获得响应流 // response = (System.Net.HttpWebResponse)request.GetResponse(); // System.IO.StreamReader myreader = new System.IO.StreamReader(response.GetResponseStream(), Encoding.UTF8); // string responseText = myreader.ReadToEnd(); // myreader.Close(); // // MessageBox.Show(responseText); // return responseText; //} public static ToJsonMy ToObj(string responeString) { ToJsonMy tjData = new ToJsonMy(); JavaScriptSerializer js = new JavaScriptSerializer(); //实例化一个能够序列化数据的类 tjData = js.Deserialize <ToJsonMy>(responeString); return(tjData); }
//请求定时器 private void timer1_Tick(object sender, EventArgs e) { string url = GetAppConfig("GetUrl"); string userId = GetAppConfig("UserId"); if (url == "" || userId == "") { MessageBox.Show("请在配置文件中配置请求url和用户UserId"); } else { string responseVal = HttpGet(url); if (responseVal == "") { // MessageBox.Show("请求异常"); return; } ToJsonMy obj = ToObj(responseVal); if (obj.code != "00") { // MessageBox.Show("请求异常"); return; } if (obj.data.total == "0") { type = 0; this.richTextBox1.Text = ""; this.richTextBox1.AppendTextColorful("暂无待办", Color.Black, 16); } else { System.Media.SoundPlayer player = new System.Media.SoundPlayer(); player.SoundLocation = Application.StartupPath + "\\yinpin.wav"; player.LoadAsync(); player.PlaySync(); type = 1; this.timer.Enabled = true; this.richTextBox1.Text = ""; string msg = "总消息数:" + obj.data.total + "\r\n"; //this.richTextBox1.AppendTextColorful(msg, Color.Black, 16); int index = 1; List <ToJsonItem> items = obj.data.items; foreach (ToJsonItem item in items) { msg += index + "、" + item.title + "\r\n"; // this.richTextBox1.AppendTextColorful(index+"、" + item.title, Color.Black, 16); //this.richTextBox1.AppendTextColorful("申请人:" + item.apply + " 时间:" + item.date, Color.Black, 14); msg += "申请人:" + item.apply + " 时间:" + item.date + "\r\n"; index++; } this.richTextBox1.AppendTextColorful(msg, Color.Black, 14); richTextBox1.SelectionStart = 0; richTextBox1.Focus(); } } }