• 转 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  
                {  
                   
                    
                }    
  • 相关阅读:
    PHP生成名片、网址二维码
    利用google api生成二维码名片
    PHP实现视频文件上传完整实例
    delphi程序中定义热键
    如何制作手机自适应网页
    取MAC地址 (含多网卡),最好的方法,支持Vista,Win7
    PHP压缩与解压Zip(PHPZip类)
    如何在mysql中存储音乐和图片文件
    viewFlipper 之二
    ViewFlipper
  • 原文地址:https://www.cnblogs.com/zhangwei99com/p/7756438.html
Copyright © 2020-2023  润新知