開源代碼: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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM