WebEnh

.net7 mvc jquery bootstrap json 学习中 第一次学PHP,正在研究中。自学进行时... ... 我的博客 https://enhweb.github.io/ 不错的皮肤:darkgreentrip,iMetro_HD
随笔 - 1079, 文章 - 1, 评论 - 75, 阅读 - 174万
  首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

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

Posted on   WebEnh  阅读(3910)  评论(0编辑  收藏  举报

今天介绍一个很好用的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

编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示

喜欢请打赏

扫描二维码打赏

了解更多