I bet there might be some nice library out there, but when I need to get somethiung done I always take the easy way out - so here I poresent my quick and dirty code for posting a blog entry to your livejournal blog from C#:
I started out with how I wanted the C# code to look:
protected void LiveJournalPost(string sHeader, string sTxt, string Username, string Password, string RpcUrl)
{
}
And here's that code:
...
string sTxtPostData = @"
";
WebHelper.PostXml(RpcUrl, sTxtPostData);
...
Building up the XML the RPC endpoint at Livejournal wants to see by just creating a long string.
And here's the WebHelper.PostXml code:
public static string PostXml(string url, string sPostData)
{
string sRet = "ERROR";
System.Net.WebClient oCli = null;
try
{
oCli = new WebClient();
oCli.Headers.Add("Content-Type", "text/xml");
byte[] byteArray = Encoding.ASCII.GetBytes(sPostData);
byte[] responseArray = oCli.UploadData(url, "POST", byteArray);
sRet = Encoding.ASCII.GetString(responseArray);
}
catch (WebException ex)
{
//Console.Write(ex.Message);
}
if (oCli != null)
{
oCli.Dispose();
oCli = null;
}
return sRet;
}
So, calling it is like:
LiveJournalPost("The post header", "And some post content bla bla", "ljusername", "ljpassword", "http://www.livejournal.com/interface/xmlrpc");
No comments:
Post a Comment