[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
Nuget
http://nuget.org/packages/Nonocast.Http