首页 > 其他 > 详细

[HTTP]Nonocast.http post应用方法

时间:2014-03-13 14:19:26      阅读:543      评论:0      收藏:0      [点我收藏+]

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);
        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; }
}

 

输出:

bubuko.com,布布扣

 

代码:

 

引用:

Source Code

http://nodata.codeplex.com/

Nuget

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

[HTTP]Nonocast.http post应用方法,布布扣,bubuko.com

[HTTP]Nonocast.http post应用方法

原文:http://www.cnblogs.com/tmywu/p/3597478.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!