• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C#开发系列(三)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

参考:http://stackoverflow.com/questions/11248935/passing-values-to-a-put-json-request-in-c-sharp

 

发送http 的 PUT 请求, 写入寄存器的值。

//put /test/putRegs

        public string putRegs()
        {
            // Create the web request
            HttpWebRequest request = WebRequest.Create("http://*****/devices/22052/regs") as HttpWebRequest;

            // Add authentication to request
            string _auth = string.Format("{0}:{1}", "**username**", "**password**");
            string _enc = Convert.ToBase64String(Encoding.ASCII.GetBytes(_auth));
            string _cred = string.Format("{0} {1}", "Basic", _enc);
            request.Headers[HttpRequestHeader.Authorization] = _cred;
            
            // Set type to POST
            request.Method = "PUT";
            request.ContentType = "application/json";

           
            using (var streamWriter = new StreamWriter(request.GetRequestStream()))
            {
                string json = "[{\"addr\":2100,\"value\":99}]";// Need to put data here to pass to the API.**

                streamWriter.Write(json);
            }

            // Get response
            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                // Get the response stream
                StreamReader reader = new StreamReader(response.GetResponseStream());

                // Console application output
                //Console.WriteLine(reader.ReadToEnd());
                return (reader.ReadToEnd());
            }
        } 

  编译运行后,浏览器访问: http://localhost:30921/test/putRegs

 写入成功!


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C# 监控windows服务,针对因异常停止的服务进行重启发布时间:2022-07-10
下一篇:
C# Socket编程(3)编码和解码发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap