2 Star 1 Fork 0

openex / httplib

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
README.md 4.03 KB
一键复制 编辑 原始数据 按行查看 历史
James Thorne 提交于 2015-07-27 23:12 . modified readme

HttpLib is a free (Apache 2.0 License) web request helper for .Net that makes it easier for developers to access and download resources from the internet.

Master build build status master

Version 2.0.12 2.0.12

About

The library was first released in 2012 and has since had over 5500 downloads.

Download

The latest releases are available on CodePlex.

Source code

Source code is available on GitHub

Latest Release:

The most recent release is 2.0.12 which supports the following features:

  • Supports most HTTP Verbs: GET / POST / PUT / DELETE and more
  • Upload and download files to disk
  • Progress monitor for file uploads/downloads
  • Completely asynchronous operation
  • Custom authentication providers can be added (currently supports basic auth)
  • Content stream can be customised
  • Cookies are static and persist between requests

Supported platforms: .Net4.0+ (WinForms, WCF, ASP.Net, Silverlight 5, Windows Phone 8.0+, Windows 8.0+).

Upcoming Releases:

2.1.13: OAuth2 authentication provider

Anonymous Usage Statistics

Anonymous usage statistics may be collected by the library. This can be disabled by calling Collector.DisableCollector().Disable() on app startup.

Examples

GET Web Page

Performs a HTTP GET on a given URL and executes the lambda function provided to the OnSuccess method. This example prints the content of the web page to the command line.

Http.Get("https://jthorne.co.uk/httplib").OnSuccess(result =>
{
	Console.Write(result);
}).Go();

Errors can be caught through using the OnFail method as show below:

Http.Get("https://jthorne.co.uk/httplib").OnSuccess(result =>
{
    Console.Write(result);
}).OnFail(webexception =>
{
Console.Write(webexception.Message);
}).Go();

POST Web Page

Web page form data can be posted to a web service using the .Form method as shown below. From user requests, this method also supports posting of dictionaries.

Http.Post("https://jthorne.co.uk/httplib").Form(new { name = "James", username = "j6mes" }).Go();

Alternatively, a raw message (such as SOAP or JSON) can be posted using the .Body method.

Upload File to Web Service

Multiple files from the local computer can be uploaded to the remote server through specifying a list of NamedFileStreams in the .Upload method.

Http.Post("https://jthorne.co.uk/httplib")
.Upload(files:
	new[] { 
		new NamedFileStream("myfile", "photo.jpg", "application/octet-stream", File.OpenRead(@"C:\photo.jpg"))
			}).Go();

And of course, fitting with the true flexibility of HttpLib, a progress monitor and onsuccess method can be added too:

Http.Post("https://jthorne.co.uk/httplib")
	.Upload(
		files:
			new[] { 
				new NamedFileStream("myfile", "photo.jpg", "application/octet-stream", File.OpenRead(@"C:\photo.jpg"))
				}, 
				onProgressChanged:
				(bytesSent, totalBytes) => 
				{
					Console.WriteLine("Uploading: " + (bytesSent / totalBytes)*100 + "% completed");
				})
				.OnSuccess(result=>
				{
					Console.WriteLine(result);
				}).Go();

Download file from Web Service

Files can be downloaded directly to disk using the DownloadTo extension. An OnSuccess method can be added as a parameter here.

If the server doesn’t reply with a content length header, the totalBytes value will be null meaning that you won’t be able to give a percentage of how much of the file has been downloaded.

Http.Get("https://jthorne.co.uk/httplib").DownloadTo(@"C:\httplib.html", onProgressChanged: (bytesCopied,totalBytes) =>; 
{
	if (totalBytes.HasValue)
	{
		("Downloaded: " + (bytesCopied/totalBytes)*100 + "%");
	}
	Console.Write("Downloaded: " + bytesCopied.ToString() + " bytes");
	},
		onSuccess: (headers) =>
	{
		UpdateText("Download Complete");
	}).Go();
C#
1
https://gitee.com/openex/httplib.git
git@gitee.com:openex/httplib.git
openex
httplib
httplib
master

搜索帮助