jonahxuworld
2021-11-02 14:50
采纳率: 95.7%
浏览 44

C#winform如何传值给http串口数据呢?要传的数据在textbox里面!

这是我这里HTTP串口打开的数据,msg是我要传入的数据!
{"code":0,"msg":"请输入正确的参数","time":"1635835468","data":null}

然后我的代码这么写,再打开http并没有什么变化!
想把textbox上的数据传过去


            string serviceAddress = "我的网址";
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceAddress);

            request.Method = "POST";
            request.ContentType = "application/json";
            string strContent = @"{ ""msg"": ""没什么""}";
            using (StreamWriter dataStream = new StreamWriter(request.GetRequestStream()))
            {
                dataStream.Write(strContent);
                dataStream.Close();
            }
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            string encoding = response.ContentEncoding;
            if (encoding == null || encoding.Length < 1)
            {
                encoding = "UTF-8"; //默认编码  
            }
            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
            string retString = reader.ReadToEnd();
            //解析josn
            JObject jo = JObject.Parse(retString);
            textBox11.Text = (jo["time"].ToString());

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题