Introduction

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: http://www.sunfinedata.com/
 * 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() 
		{
			giftWriter.Close();
		}

		private Int32 OnWriteData(Byte[] buf, Int32 size, Int32 nmemb, Object extraData)
		{
	        	giftWriter.Write(System.Text.Encoding.UTF8.GetString(buf));
	        	return size * nmemb;
		}

		public void DownloadURL(string url, string urlPostFields, string OutputFilename, bool isPost)
		{
			outputFilename = OutputFilename;
			if ( outputFilename != "") { giftWriterOpen(); }
			try 
			{
            	Curl.GlobalInit((int)CURLinitFlag.CURL_GLOBAL_ALL);
	            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);
	            	easy.SetOpt(CURLoption.CURLOPT_POSTFIELDS,urlPostFields);
	            }
	            easy.SetOpt(CURLoption.CURLOPT_USERAGENT,
	                "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);
	            }
	            easy.Perform();
	            easy.Cleanup();
	            Curl.GlobalCleanup();
	        }
	        catch(Exception ex) { }
			if ( outputFilename != "") { giftWriterClose(); }
		}
	}
}

Download

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

Reference

Share

Tags: , , ,