现在的位置: 首页 > 综合 > 正文

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

2018年05月20日 ⁄ 综合 ⁄ 共 1564字 ⁄ 字号 评论关闭

 

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
            {
             
              
            }  

 


 


 

抱歉!评论已关闭.