When I am working on TwitterGIFT project, I need find how to use Twitter API in my C# project. I know CURL is really good at Linux environment. My PERL script use it to access Twitter API functions. It is powerful and convenient and easy to use. How can I use it in my C# project? Through system calling. That will be pretty bad. I explore the curl’s official site and find out they provide library to different development environments. One of them is for .NET, which is what I am looking for. I download it and try hard to find how to use it. At the same time, I searched the internet to find some useful example. Yes, I found some thing and use them as my start point. Then I developed a complete solution for my C# project. I publish it here to share with developers worldwide.

Source code

 * Author: Zhanshan Dong
 * Website:
 * Date: 4/3/2009
 * Modified: 9/18/2009

using System;
using SeasideResearch.LibCurlNet;
using System.IO;  

namespace TwitterGIFT
	/// This is a complete C# class to utilize LibCurlNet
	/// You can use it in your project freely as long as 
	///    1) keep the identity of the class
	///    2) keep the authorization information
	///    3) use it in open source
	///    4) do not use it in commercial environment
	public class GiftCURL
		private StreamWriter giftWriter;
		private string outputFilename;
		private string username;
		private string password;

		public GiftCURL( string userName, string passWord)
			username = userName;
			password = passWord;
		private void giftWriterOpen() 
			giftWriter = new StreamWriter(outputFilename);
		private void giftWriterClose() 

		private Int32 OnWriteData(Byte[] buf, Int32 size, Int32 nmemb, Object extraData)
	        	return size * nmemb;

		public void DownloadURL(string url, string urlPostFields, string OutputFilename, bool isPost)
			outputFilename = OutputFilename;
			if ( outputFilename != "") { giftWriterOpen(); }
	            Easy easy = new Easy();
	            Easy.WriteFunction wf = new Easy.WriteFunction(OnWriteData);
	            easy.SetOpt(CURLoption.CURLOPT_WRITEFUNCTION, wf);
	            easy.SetOpt(CURLoption.CURLOPT_POST, isPost);
	            if (urlPostFields != "") 
	            	easy.SetOpt(CURLoption.CURLOPT_POST, true);
	                "Mozilla 4.0 (compatible; MSIE 6.0; Win32");
	            easy.SetOpt(CURLoption.CURLOPT_FOLLOWLOCATION, true);
	            if (url != "") { easy.SetOpt(CURLoption.CURLOPT_URL,url); }
	            if ((username !="") && (password != "")) 
	            	easy.SetOpt(CURLoption.CURLOPT_USERPWD,username + ":" +password);
	        catch(Exception ex) { }
			if ( outputFilename != "") { giftWriterClose(); }


you can download the source code in file – GiftCURL.cs



Tags: , , ,