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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 |
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(); } //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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 |
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); 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 ; } } |
输出:
代码:戳
引用:
http://nuget.org/packages/Nonocast.Http
[HTTP]Nonocast.http post应用方法,布布扣,bubuko.com
原文:http://www.cnblogs.com/tmywu/p/3597478.html