[HTTP]Nonocast.http post方法

本文介绍了如何使用Nonocast.Http库进行HTTP POST请求,并处理序列化的数据内容。通过代码示例展示了客户端如何发送JSON序列化的设备信息到服务器,并接收响应。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Nonocast.Http is a free, open source developer focused web service via http for small and medium software.

The library implement HTTP1.1 protocal and also support WebSockets.

 

在项目中需要Post content body。body包含一些序列化数据。

 

Server:

    public class Program : SmallHTTPServiceBase
    {
        static void Main(string[] args)
        {
            new Program().Run();

            Console.WriteLine("press any key to exit.");
            Console.ReadLine();
        }

        private void Run()
        {
            this.Open();
        }

        // http://localhost:7005/Action/Default
        //public ActionResult Default()
        //{
        //    return new ContentResult("<h1>hello world</h1>");
        //}


        public ActionResult Default(Arguments arg)
        {
            var ctx = ChannelContext.Current;
            byte[] buffer = new byte[ctx.ContentLength];
            ctx.Stream.Read(buffer, 0, ctx.ContentLength);
            Console.WriteLine(Encoding.UTF8.GetString(buffer));

            return new ContentResult("ok");
        }

    }

 

Client:

 

    public class Program
    {
        static void Main(string[] args)
        {
            new Program().Run();

            Console.WriteLine("press any key to exit.");
            Console.ReadLine();
        }


        private void Run()
        {
            Device d = new Device { Id = "ssdasdsadadadsadqweqwe", Version = null };
            string data = JsonConvert.SerializeObject(d);
            string url = string.Format(@"http://localhost:7005/Action/Default");
            HttpWebResponse result = null;
            HttpStatusCode statusCode = HttpStatusCode.NotFound;
            using (result = (HttpHelper.RawPostByJson(url, data) as HttpWebResponse))
            {
                if (result != null)
                {
                    statusCode = result.StatusCode;
                }

            }
        }
    }

    public class Device
    {
        public string Id { get; set; }
        public string Version { get; set; }
    }

 

输出:

 

代码:

 

引用:

Source Code

http://nodata.codeplex.com/

Nuget

http://nuget.org/packages/Nonocast.Http

 

转载于:https://www.cnblogs.com/tmywu/p/3597478.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值