標籤:com nbsp 產生 原始碼下載 擷取資料 web服務 bug 開原始碼 for
今天介紹一個很好用的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
開原始碼:Http請求封裝類庫HttpLib介紹、使用說明