转 winfrom如何通过http来进行通信,并且通过传递json格式的数据可接受json格式的数据

来源:互联网 发布:樱井知香喷泉图片欣赏 编辑:程序博客网 时间:2024/04/29 05:15
 

winfrom如何通过http来进行通信,并且通过传递json格式的数据可接受json格式的数据

 

string username = this.textBox1.Text;            string password = this.textBox2.Text;            string AA = HttpUtility.UrlEncode(username, Encoding.UTF8);            string bb = HttpUtility.UrlEncode(password, Encoding.UTF8);            ASCIIEncoding encoding = new ASCIIEncoding();            String content = "";            try            {                string json = "{\"uname\":\"" + AA + "\",\"psw\":\"" + bb + "\",\"param\":\"login\"}";                MessageBox.Show(json);                JObject o = JObject.Parse(json);                String param = o.ToString();                byte[] data = encoding.GetBytes(param);                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://192.168.10.30:8080/ss/aa.do?method=login");                request.KeepAlive = false;                request.Method = "POST";                request.ContentType = "application/json;characterSet:UTF-8";                request.ContentLength = data.Length;                Stream sm = request.GetRequestStream();                sm.Write(data, 0, data.Length);                sm.Close();                HttpWebResponse response = (HttpWebResponse)request.GetResponse();                Stream streamResponse = response.GetResponseStream();                StreamReader streamRead = new StreamReader(streamResponse, Encoding.UTF8);                Char[] readBuff = new Char[256];                int count = streamRead.Read(readBuff, 0, 256);                while (count > 0)                {                    String outputData = new String(readBuff, 0, count);                    content += outputData;                    count = streamRead.Read(readBuff, 0, 256);                }                response.Close();                MessageBox.Show(content);                string jsons = content;                JObject jobject = JObject.Parse(jsons);                JsonReader read = jobject.CreateReader();                MessageBox.Show(read.ToString());                               MessageBox.Show(jobject.ToString());                //Dictionary<string, string>[] companies = content.Deserialize<Dictionary<string, string>[]>(content);                //foreach (var item in companies)                //{                //    MessageBox.Show(item);                //}            }            catch (Exception ex)            {            }            finally            {                                       }