• 开源代码:Http请求封装类库HttpLib介绍、使用说明


    今天介绍一个很好用的Http请求类库--Httplib。一直以来,我们都是为了一次web请求,单独写一段代码

    有了这个类,我们就可以很方便的直接使用了。

    项目介绍:

    http://www。suchso。com/UIweb/jquery-Plupload-use.html

    基于C#语言,Httplib让异步交互处理数据更容易了。类库的方法包括:上传文件到服务器,获取页面数据。

    该项目专门为web服务写的,如果你打算重新写一个http客户端和服务端的话,建议你使用wcf。

    最新代码下载:http://jthorne.co.uk/blog/httplib/building-a-windows-store-class-library-for-httplib/

    支持的方法有:

    • GET

    • POST

    • PUT

    • HEAD

    • DELETE

    • Upload - PUT or POST

    用法:

    引用dll

    using Redslide.HttpLib

    从web服务获取数据

    Request.Get("http://jthorne.co.uk/", 
    result=>
    {
        Console.Write(result);
    });
    

    从服务器下载数据

    Request.Get("http://cachefly.cachefly.net/100mb.test", 
    (headers, result) =>
    {
            FileStream fs = new FileStream(@"C:100mb.test", FileMode.OpenOrCreate);
            result.CopyTo(fs);
            fs.Close();
    });
    

    Post数据给web服务

    Request.Post("http://testing.local/post.php", new {name="james",username="Redslide"}, 
    result=>
    {
        Console.Write(result);
    });
    



    Post数据给web服务,并且捕获异常

    Request.Post("http://testing.local/post.php", new { name = "value"}, 
    result=>
    {
        Console.Write(result);
    }, 
    e=>
    {
        Console.Write(e.ToString());
    });
    



    上传文件到服务器

    Request.Upload("http://testing.local/post.php", new {name = "value"}, new[] {
        new NamedFileStream("file", "photo.jpg", "image/jpeg", new FileStream(@"C:photo.jpg",FileMode.Open))
    }, 
    result=>
    {
        Console.Write(result);
    });
    

    用起来还是很简单的。这样分装好的类库,大大的减少了我们的工作量,并且因为有团队维护和很好的异常处理,

    减少bug的产生。当然你要是不放心,你可以阅读源代码,重新编译。源代码下载地址:http://httplib.codeplex.com/SourceControl/latest

  • 相关阅读:
    c#数据结构与算法
    学习资源---.NET
    怎样完全删除sqlserver
    树,森林 二叉树之间转化 原理
    ref 和out 区别
    GridView批量删除记录、全选及弹出确认对话框
    .NET基础 小记--------2013.8.10
    Xml 读写
    同步 异步 区别
    委托学习
  • 原文地址:https://www.cnblogs.com/webenh/p/6143418.html
Copyright © 2020-2023  润新知