博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
转 winfrom如何通过http来进行通信,并且通过传递json格式的数据可接受json格式的数据...
阅读量:4993 次
发布时间:2019-06-12

本文共 2601 字,大约阅读时间需要 8 分钟。

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
[] companies = content.Deserialize
[]>(content); //foreach (var item in companies) //{ // MessageBox.Show(item); //} } catch (Exception ex) { } finally { }

 

转载于:https://www.cnblogs.com/zhangwei99com/p/7756438.html

你可能感兴趣的文章
POJ 1751 Highways(最小生成树&Prim)题解
查看>>
linux 安装openssh-server, openssh-client
查看>>
Java继承的基本概念及其限制 总结
查看>>
RF1001: 各浏览器对 '@font-face' 规则支持的字体格式不同,IE 支持 EOT 字体,Firefox Safari Opera 支持 TrueType 等字体...
查看>>
Socket 学习(三)
查看>>
题解 CF43B 【Letter】
查看>>
CommandName and CommandArgument
查看>>
[z]FNV哈希算法
查看>>
通过层序和中序遍历序列重建二叉树
查看>>
【Git】git clone与git pull区别
查看>>
【SVN】SVN的trunk、branches、tag的使用以及分支的概念
查看>>
JS闭包理解
查看>>
整数对题目
查看>>
php设计模式-观察者模式
查看>>
NFC技术:使用Android Beam技术传输文本(一)
查看>>
C++判断一个文件是否可以正确打开的代码
查看>>
unity 判断 是手机还是平板
查看>>
VisualStudio2015单步调试
查看>>
【进程资源】监视进程资源
查看>>
团队成员效绩评定
查看>>