zoukankan      html  css  js  c++  java
  • [HTTP]Nonocast.http post方法

    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

  • 相关阅读:
    [moka同学笔记]yii2.0缓存
    [moka同学笔记]yii2.0查询数据库
    [moka同学笔记]yii2.0数据库操作以及分页
    [moka同学笔记]yii2.0表单的使用
    [moka同学笔记]bootstrap基础
    yii2时间日期控件的使用[转]
    java基础练习[一]
    上传照片
    selenium截图功能
    pip更新报错问题
  • 原文地址:https://www.cnblogs.com/tmywu/p/3597478.html
Copyright © 2011-2022 走看看