Click here to Skip to main content
13,356,318 members (59,015 online)
Click here to Skip to main content
Add your own
alternative version


35 bookmarked
Posted 20 Jan 2005

Blogging to Using C# Code

, 20 Jan 2005
Rate this:
Please Sign up or sign in to vote.
This article shows you how to blog programmatically to using C#.


Web-logging, also known as blogging, is a new technology that allows you to publish your ideas to the world quickly. This has been a trend for the last several months. This article will show you how to post blogs to programmatically using C#, so that you can implement the functionality in your own application.

You need some familiarization with C# in order to understand the code.

Background is one of the most popular free blog hosts today. That's why I chose this as code sample, because more people can benefit from it. You can always get a free LiveJournal blog account here.

The code provided here allows you to blog programmatically. You can go straight to the code. However, I would also like to show you how it works behind the scene.

You are probably familiar with blogging. If you are not, let me take you through the process of blogging, step-by-step using your mouse and keyboard.

In general, these are the steps you need to do, to blog manually:

  • Go to the blog host's homepage.
  • Log-in using your username and password (i.e., "my_favorite_username" and "my_favorite_password").
  • Enter your blog's subject and your blog's body (i.e., "This is a title." and "This is a message body."), click Submit.
  • Check your blog (i.e., going to

The fact is, only 2nd and 3rd are necessary in the code. These are the two major steps, in which you have to send data to the blog host.

So, what's behind the scene?

Here are the steps performed by your browser, and the code I am going to provide does the following using C#:

  • Send a HttpWebRequest including username and password.
  • Receive a password validation response from the blog server.
  • If log-in is successful, store the response's cookies.
  • Send another HttpWebRequest including the previously stored cookies, a blog title and a message body.

Using the code

I will show you the most important section of the code. This block of code allows you to send your username and password to the blog server, i.e. the log-in process. After successfully logging-in, you may use similar code to send the blog title and the message body to the blog server. Comments have been added to the code:

public bool Login(string username, string password)
      // Prepare the HttpWebRequest to send data to 
      string url = 
      HttpWebRequest request = 
        (HttpWebRequest) WebRequest.Create(url);
      request.UserAgent = "Mozilla/5.0 (Windows; U; " 
         + "Windows NT 5.1; en-US; rv:1.7) "
         + "Gecko/20040707 Firefox/0.9.2";
      request.Method = "POST";
      request.Accept = 
        + "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
      request.KeepAlive = true;
      request.ContentType = @"application/x-www-form-urlencoded";
      request.Referer = 
      request.CookieContainer = cookieContainer;

      // Send the log-in data
      string postData = 
           + "password={1}&expire=close&"
           + "bindip=no&action%3Alogin=Log+in...", username,"
           + " password); 
      byte [] postBuffer = 
      request.ContentLength = postBuffer.Length;
      Stream postDataStream = request.GetRequestStream();

      // Get the response
      HttpWebResponse response =
        (HttpWebResponse) request.GetResponse();
      response.Cookies =

      // Add the cookie to this instance of object, 
      //so that the cookies can be reused for next request

      // Read the response from the stream
      Encoding enc = System.Text.Encoding.UTF8;
      StreamReader responseStream = 
        new StreamReader(response.GetResponseStream(), enc, true);

      string responseHtml = responseStream.ReadToEnd();

      // For debug purpose, print out the cookies received from 
      foreach (Cookie c in response.Cookies)
         Debug.WriteLine(string.Format("{0}: {1}", c.Name, c.Value));

      // Check if log-in is successful
      string successString = 
         "you are now logged in to";
      if (responseHtml.IndexOf(successString) >= 0)
         // log-in successfully
         return true;
         // log-in failed
         return false;
   catch (Exception)
      return false;

And here is the code, to call this function in my main application:

static void Main(string[] args)
   /* Put your configuration below */
   string username = "YOUR LIVEJOURNAL USERNAME";
   string password = "YOUR LIVEJOURNAL PASSWORD";
   string subject = "This is subject";
   string body = "This is a message body";
   /* End of configuration */

   // Create the LiveJournal object
   LiveJournal lj = new LiveJournal();

   // Log-in using your username and password
   if (lj.Login(username, password))    //  <--- calling the code above
      // if log-in successfully, post the blog
      lj.AddBlog(subject, body);
      System.Console.WriteLine("Blog successfully!");
      System.Console.WriteLine("Blog failed");
   System.Console.WriteLine("Press any key to close the application...");

The sample source file, you downloaded from the above link, shows the complete code and, how everything works together.

Points of Interest

You probably must have wondered, if the same code works on other blog servers, the answer is yes and no. Yes, it can, but you have to change some code. For example, the data being sent to different blog servers are different. You need to have some network level programming knowledge in order to get these done. There are programs to help you, such as Ethereal or Paros.

Security Notes

There are several points worth mentioning. While you may find the code useful because you can add blogging functionalities to your e-mail clients and instant messenger clients, this (along with some creativity and imagination) can be misused and it can cause security threats:

  • Spoofing Identity: Adversary can brute force username and password programmatically, possibly using dictionary attack.
  • Tampering of Data: Adversary can change the content of cookies and session keys. This is also known as cookie poisoning.
  • Information Disclosure: Adversary can collect information about how data is passed between pages.
  • Denial of Service: Adversary can create arbitrary number of threads to log in at the same time, thus consuming all of server resources.
  • Elevation of Privilege: Adversary may be able to change request type, to carry out operations which may not be permitted to that user account. For example, the adversary might inject HTML tags that are supposed to be restricted by the client browser.


It is difficult for me, to try to explain everything. Please ask me, if there is anything that is not clear. I will try to answer them as clearly as possible.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Kelvin Tsang
Canada Canada
Kelvin Tsang is a software security guru who likes to play bridge when he is free.

You may also be interested in...


Comments and Discussions

GeneralI cannot blog with this now Pin
collecter16-Jun-10 5:16
membercollecter16-Jun-10 5:16 
GeneralRe: Try This, It Really Works Pin
Вячеслав Авсенёв5-Jun-13 22:41
memberВячеслав Авсенёв5-Jun-13 22:41 
Generalnot working with the similar code to connect to asp chat server Pin
niqing14-Jul-07 12:36
memberniqing14-Jul-07 12:36 
GeneralUgly but working solution Pin
ASPCode.net12-Jun-07 3:51
memberASPCode.net12-Jun-07 3:51 
Generalthe code doesnt work Pin
lol187817-May-07 6:48
memberlol187817-May-07 6:48 
QuestionUploading files in similar way? Pin
alex991317-Jul-06 4:22
memberalex991317-Jul-06 4:22 
QuestionVery useful idea. Have you tried to use it on, say, Yahoo email? Pin
tank2097-May-06 15:05
membertank2097-May-06 15:05 
AnswerRe: Very useful idea. Have you tried to use it on, say, Yahoo email? Pin
Kelvin Tsang7-May-06 16:56
memberKelvin Tsang7-May-06 16:56 
GeneralIts not working Pin
saishyam1-Mar-06 20:36
membersaishyam1-Mar-06 20:36 
GeneralRe: Its not working Pin
Kelvin Tsang2-Mar-06 5:57
memberKelvin Tsang2-Mar-06 5:57 
Generalsome observtions Pin
caiafaverde21-Feb-05 23:33
membercaiafaverde21-Feb-05 23:33 
GeneralRe: some observtions Pin
Kelvin Tsang22-Feb-05 6:03
memberKelvin Tsang22-Feb-05 6:03 
Questionhow to post data other than IIS server such as Apachi Pin
MohdYounisKhan17-Feb-05 1:08
memberMohdYounisKhan17-Feb-05 1:08 
AnswerRe: how to post data other than IIS server such as Apachi Pin
Kelvin Tsang24-Feb-05 20:39
memberKelvin Tsang24-Feb-05 20:39 
GeneralInteresting, it would be better if... Pin
zumanity029-Jan-05 15:50
memberzumanity029-Jan-05 15:50 
GeneralRe: Interesting, it would be better if... Pin
Kelvin Tsang29-Jan-05 16:43
memberKelvin Tsang29-Jan-05 16:43 
GeneralNot a bad read, albeit a little short. Pin
Adam Goossens21-Jan-05 0:54
memberAdam Goossens21-Jan-05 0:54 
GeneralRe: Not a bad read, albeit a little short. Pin
Kelvin Tsang21-Jan-05 7:45
memberKelvin Tsang21-Jan-05 7:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180111.1 | Last Updated 21 Jan 2005
Article Copyright 2005 by Kelvin Tsang
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid