Archive

Archive for October, 2006

Useful c# function – remove duplicates

This is a useful c# function, that removes duplicates from an arraylist
 

public ArrayList RemoveDups(ArrayList items)

{

ArrayList noDups = new ArrayList();

foreach(string strItem in items)

{

if (!noDups.Contains(strItem.Trim()))

{

noDups.Add(strItem.Trim());

}

}

noDups.Sort();

return noDups;

}

 

Handy!

Categories: Uncategorized

del.icio.us auto-post

This is a bit of code I wrote that will automatically post a website to delicious.  The HttpRequest Class is outside of the scope of this post, but you should get the jist of how it’s done.
 

private void SubmitDelicious(string url,string description,string username,string password,string tags)

{

#region

postdata

/*

POST /login HTTP/1.1

Host: secure.del.icio.us

user_name=nora344

password=lokiju

jump=no

url=http%3A%2F%2Fwww.envoyez.com

login=log+in

GET /nora344?url=http%3A%2F%2Fwww.envoyez.com

jump=no

POST /nora344?636480 HTTP/1.1

Host: del.icio.us

url=http%3A%2F%2Fwww.envoyez.com

oldurl=http%3A%2F%2Fwww.envoyez.com

description=envoyez+sms

notes=

tags=

jump=no

key=6c7b3c42b196c8e3ebc90130317786eb

*/

#endregion

// step 1. login

string strUrl = "https://secure.del.icio.us/login";

string strPostdata = "user_name=" + username + "&";

strPostdata += "password=" + password + "&";

strPostdata += "jump=no&";

strPostdata += "url=" + HttpUtility.UrlEncode(url) + "&";

strPostdata += "login=log+in&";

HTTPRequest hrWeb =

new HTTPRequest();

hrWeb.blnExpect100Continue = false;

string html = hrWeb.Request(strUrl,"POST",strPostdata);

// at this point, we may need to transfer cookies from secure.delicious to delicious?

CookieCollection ckCol = hrWeb.RequestCookies.GetCookies(

new Uri("https://secure.del.icio.us"));

 

Cookie ckAccept = new Cookie("_accept_s_cookies","1","/",".icio.us");

Cookie ckInstall =

new Cookie("_ext_install_redir","1","/",".icio.us");

Cookie ckRegister =

new Cookie("_register_id","1248967165","/",".icio.us"); // _register_id?

Cookie ckUser =

new Cookie("_user",ckCol["_user"].Value,"/",".icio.us");

Cookie ckBM = new Cookie("_bm_url",HttpUtility.UrlEncode(url),"/",".icio.us");

// BX=4ehl0pt2i6vse&b=3&s=dr – already set

hrWeb.RequestCookies.Add(ckAccept);

hrWeb.RequestCookies.Add(ckInstall);

hrWeb.RequestCookies.Add(ckRegister);

hrWeb.RequestCookies.Add(ckUser);

hrWeb.RequestCookies.Add(ckBM);

// step 2. redirect

strUrl = "http://del.icio.us/";

strUrl += username;

strUrl += "?url=" + HttpUtility.UrlEncode(url) + "&jump=no";

html = hrWeb.Request(strUrl);

if (html.IndexOf("in order to save an item, you have to log in")!=-1)

{

return;

}

string strIDRegex = @"action…w+?(?<ID>d+)";

string strID = Regex.Match(html,strIDRegex).Groups["ID"].Value;

string strKeyRegex = @"key..value..(?<KEY>w+)";

string strKey = Regex.Match(html,strKeyRegex).Groups["KEY"].Value;

// step 3. post

strUrl = "http://del.icio.us/&quot; + username + "?" + strID;

strPostdata = "url=" + HttpUtility.UrlEncode(url) + "&";

strPostdata += "oldurl=" + HttpUtility.UrlEncode(url) + "&";

strPostdata += "description=" + HttpUtility.UrlEncode(description) + "&";

strPostdata += "notes=&";

strPostdata += "tags=" + HttpUtility.UrlEncode(tags) + "&";

strPostdata += "jump=no&";

strPostdata += "key=" + strKey;

html = hrWeb.Request(strUrl,"POST",strPostdata);

}

 

Categories: Uncategorized