使用 C# 代码博客到 LiveJournal.com






4.23/5 (10投票s)
2005年1月21日
3分钟阅读

87137

255
本文将向您展示如何使用 C# 以编程方式博客到 LiveJournal.com。
引言
网络日志(又称博客)是一项新技术,可让您快速地向世界发布您的想法。在过去的几个月里,这已成为一种趋势。本文将向您展示如何使用 C# 以编程方式将博客发布到 LiveJournal.com,以便您可以在自己的应用程序中实现此功能。
您需要熟悉 C# 才能理解代码。
背景
LiveJournal.com 是当今最受欢迎的免费博客托管之一。这就是我选择它作为代码示例的原因,因为更多人可以从中受益。您始终可以在 此处 获取免费的 LiveJournal 博客帐户。
此处提供的代码允许您以编程方式进行博客。您可以直接查看代码。但是,我也想向您展示它是如何工作的。
您可能熟悉博客。如果您不熟悉,请允许我带您一步一步地完成使用鼠标和键盘进行博客的过程。
总的来说,这是您手动博客需要执行的步骤:
- 转到 博客托管主页。
- 使用您的用户名和密码登录(即“my_favorite_username”和“my_favorite_password”)。
- 输入您的博客主题和博客正文(即“This is a title.”和“This is a message body.”),然后单击“提交”。
- 检查您的博客(即转到 http://www.livejournal.com/users/your_favorite_username/)。
事实是,在代码中只需要第 2 步和第 3 步。这是您必须将数据发送到博客托管商的两个主要步骤。
那么,幕后是什么呢?
以下是您的浏览器执行的步骤,我将提供的代码将使用 C# 执行以下操作:
- 发送一个
HttpWebRequest
,其中包含username
和password
。 - 接收来自博客服务器的密码验证响应。
- 如果登录成功,请存储响应的 cookies。
- 发送另一个
HttpWebRequest
,其中包含之前存储的 cookies、博客标题和博客正文。
使用代码
我将向您展示代码中最重要的部分。这部分代码允许您将用户名和密码发送到博客服务器,即登录过程。成功登录后,您可以使用类似的代码将博客标题和博客正文发送到博客服务器。代码中已添加注释。
public bool Login(string username, string password)
{
try
{
// Prepare the HttpWebRequest to send data to
// LiveJournal.com
string url =
string.Format("http://www.livejournal.com/login.bml");
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/xml,application/xml,application/xhtml+xml,"
+ "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 =
string.Format("http://www.livejournal.com/login.bml?nojs=1");
request.CookieContainer = cookieContainer;
// Send the log-in data
string postData =
string.Format("response=&user={0}&"
+ "password={1}&expire=close&"
+ "bindip=no&action%3Alogin=Log+in...", username,"
+ " password);
request.Method="POST";
byte [] postBuffer =
System.Text.Encoding.GetEncoding(1252).GetBytes(postData);
request.ContentLength = postBuffer.Length;
Stream postDataStream = request.GetRequestStream();
postDataStream.Write(postBuffer,0,postBuffer.Length);
postDataStream.Close();
// Get the response
HttpWebResponse response =
(HttpWebResponse) request.GetResponse();
response.Cookies =
request.CookieContainer.GetCookies(request.RequestUri);
// Add the cookie to this instance of object,
//so that the cookies can be reused for next request
Cookies.Add(response.Cookies);
// Read the response from the stream
Encoding enc = System.Text.Encoding.UTF8;
StreamReader responseStream =
new StreamReader(response.GetResponseStream(), enc, true);
string responseHtml = responseStream.ReadToEnd();
response.Close();
responseStream.Close();
// For debug purpose, print out the cookies received from
// LiveJournal.com
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 LiveJournal.com";
if (responseHtml.IndexOf(successString) >= 0)
{
// log-in successfully
return true;
}
else
{
// log-in failed
return false;
}
}
catch (Exception)
{
return false;
}
}
以下是在我的主应用程序中调用此函数的代码:
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!");
}
else
{
System.Console.WriteLine("Blog failed");
}
System.Console.WriteLine("Press any key to close the application...");
System.Console.ReadLine();
}
您从上方链接下载的示例源代码文件显示了完整的代码以及所有内容的协同工作方式。
关注点
您可能已经想过,相同的代码是否适用于其他博客服务器,答案是肯定的,也 [不是]。是的,可以,但您需要更改一些代码。例如,发送到不同博客服务器的数据是不同的。您需要具备一些网络级编程知识才能完成这些工作。有一些程序可以帮助您,例如 Ethereal 或 Paros。
安全注意事项
有几点值得一提。虽然您可能会发现此代码很有用,因为它可以在您的电子邮件客户端和即时消息客户端中添加博客功能,但(结合一些创意和想象力)也可能被滥用并造成安全威胁。
- 身份欺骗:攻击者可能通过字典攻击以编程方式暴力破解用户名和密码。
- 数据篡改:攻击者可以更改 cookies 和会话密钥的内容。这也被称为 cookie 投毒。
- 信息泄露:攻击者可以收集有关数据如何在页面之间传递的信息。
- 拒绝服务:攻击者可以同时创建任意数量的线程进行登录,从而消耗所有服务器资源。
- 权限提升:攻击者可能能够更改请求类型,以执行可能未被该用户帐户允许的操作。例如,攻击者可能会注入通常会被客户端浏览器限制的 HTML 标签。
问题?
对我来说,很难尝试解释所有内容。如果有什么不清楚的地方,请随时提问。我会尽力清晰地回答。