Monday, June 23, 2008

Lets talk about HTTP protocol, and HTTP clients

I have been looking for good source on anything HTTPS that retains cookies on multiple threads. Sure there are active x controls you can spend money on, but what is the fun in spending money? Plus there are much easier alternatives out there.

Now, everyone has heard of Curl, you know the libraries used by PHP to do HTTP, HTTPS, FTP, SFTP, and so on...Well there is also a libcurl for Visual Basic .NET, and Visual C#! Sure its a bitch to get going....but once you do, you'll be making super applications in no time. Here is some example code for C#, and libcurl. You can download the libcurl dll files here.

The example code i am about to show you is not just so that you can make HTTP posts, and HTTP get requests, its also to manage cookies in a thread safe environment....I will be making changes to this code periodically but im sure there are plenty of people interested in this source code.... here it is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SeasideResearch.LibCurlNet;
using System.IO;

class HTTP
{
private static Easy easy;
private static Random rand = new Random();
private static string SockBuff;
private static string CookieFile = AppDomain.CurrentDomain.BaseDirectory + "cookie" + rand.Next(0, 9) + rand.Next(0, 9) + rand.Next(0, 9) + rand.Next(0, 9) + rand.Next(0, 9) + rand.Next(0, 9) + rand.Next(0, 9) + ".txt";
public static string UserAgent = "Mozilla 5.0";
public static string Proxy = "";

public void Dispose()
{
ClearCookies();
}

public void CurlInit()
{
Curl.GlobalInit((int)CURLinitFlag.CURL_GLOBAL_ALL);
}

public void ClearCookies()
{
if (File.Exists(CookieFile))
{
File.Delete(CookieFile);
}

}

public string HTTPGet(string URL, string Proxy)
{
easy = new Easy();
SockBuff = "";

try
{
Easy.WriteFunction wf = new Easy.WriteFunction(OnWriteData);

easy.SetOpt(CURLoption.CURLOPT_URL, URL);
easy.SetOpt(CURLoption.CURLOPT_TIMEOUT, "60");
easy.SetOpt(CURLoption.CURLOPT_WRITEFUNCTION, wf);
easy.SetOpt(CURLoption.CURLOPT_USERAGENT, UserAgent);
easy.SetOpt(CURLoption.CURLOPT_COOKIEFILE, CookieFile);
easy.SetOpt(CURLoption.CURLOPT_COOKIEJAR, CookieFile);
easy.SetOpt(CURLoption.CURLOPT_FOLLOWLOCATION, true);

if (URL.Contains("https"))
{
easy.SetOpt(CURLoption.CURLOPT_SSL_VERIFYHOST, 1);
easy.SetOpt(CURLoption.CURLOPT_SSL_VERIFYPEER, 0);
}

if (Proxy != "")
{
easy.SetOpt(CURLoption.CURLOPT_PROXY, Proxy);
easy.SetOpt(CURLoption.CURLOPT_PROXYTYPE, CURLproxyType.CURLPROXY_HTTP);
}

easy.Perform();
easy.Cleanup();

}
catch
{
Console.WriteLine("Get Request Error");
}

return SockBuff;
}

public string HTTPPost(string URL, string Content, string Proxy)
{
easy = new Easy();
SockBuff = "";

try
{
Easy.WriteFunction wf = new Easy.WriteFunction(OnWriteData);

easy.SetOpt(CURLoption.CURLOPT_URL, URL);
easy.SetOpt(CURLoption.CURLOPT_TIMEOUT, "60");
easy.SetOpt(CURLoption.CURLOPT_WRITEFUNCTION, wf);
easy.SetOpt(CURLoption.CURLOPT_USERAGENT, UserAgent);
easy.SetOpt(CURLoption.CURLOPT_COOKIEFILE, CookieFile);
easy.SetOpt(CURLoption.CURLOPT_COOKIEJAR, CookieFile);
easy.SetOpt(CURLoption.CURLOPT_POSTFIELDS, Content);
//easy.SetOpt(CURLoption.CURLOPT_POSTFIELDSIZE, Content.Length);
easy.SetOpt(CURLoption.CURLOPT_FOLLOWLOCATION, true);
easy.SetOpt(CURLoption.CURLOPT_POST, true);


if (URL.Contains("https"))
{
easy.SetOpt(CURLoption.CURLOPT_SSL_VERIFYHOST, 1);
easy.SetOpt(CURLoption.CURLOPT_SSL_VERIFYPEER, 0);
}

if (Proxy != "")
{
easy.SetOpt(CURLoption.CURLOPT_PROXY, Proxy);
easy.SetOpt(CURLoption.CURLOPT_PROXYTYPE, CURLproxyType.CURLPROXY_HTTP);
}

easy.Perform();
easy.Cleanup();

}
catch
{

}
return SockBuff;

}

public string SafeString(string data)
{
return Curl.Escape(data, data.Length);
}

public string UnSafeString(string data)
{
return Curl.Unescape(data, data.Length);
}

public static Int32 OnWriteData(Byte[] buf, Int32 size, Int32 nmemb, Object extraData)
{
// Console.Write(System.Text.Encoding.UTF8.GetString(buf));
SockBuff = SockBuff + System.Text.Encoding.UTF8.GetString(buf);

return size * nmemb;
}
}


P.S: When you paste this code into the Visual C# IDE, it will correctly indent it for you. This code is by me and you will find it no where else. I hope you like it, leave some comments if you see things that could be improved. Thanks for everyones input!

Welcome to my .NET framework blog!

I code in both visual basic .net 2008, and Visual C#. My name is Ray Hernandez, and i am a self taught programmer, brought up in a crazy world. I didn't have everything, i don't come from a rich home, and i am one mean ass programmer. With this blog i plan to help the world, make all that is hard, easy again. I did not go to school for programming. Everything i know now i taught myself, and i am a good listener. Hopefully with this blog setup, we can do something great for the Microsoft programming community. Time to get old school in this bitch....with new toys of course :D