这几天我一个朋友在学seo,老师要求学生们同时发表一篇博客来测试百度的排名系统。所以就有了这个小程序,给同样有这方面需求的朋友们一个参考,下面介绍一下实现方法。

国内的这几个网站都实现了国际通用的博客api:MetaWeblog。通过网上提供的封装好的dll,使用非常方便,dll下载地址:

http://joeblogs.codeplex.com/releases/view/29856

下面的链接是dll的安装说明,可以看这个,我就不作介绍了:

http://www.alexjamesbrown.com/geek/development/dotnet/using-joeblogs/

还有就是想对MetaWeblog使用的XML-RPC规范有进一步了解的可以看这个文档:

http://www.duduwolf.com/post/41.asp

我也在学习中,有什么问题或者疑问欢迎提出,共同学习!

  还有很多其它网站也实现了该接口,我这里就不写了,都一样。下面介绍实现方法。

dll的调用方法:

using AlexJamesBrown.JoeBlogs; //调用dll
MetaWeblogWrapper mw = new MetaWeblogWrapper(url, username, passwd); //url是网站提供的api接口,username为博客用户名,passwd是密码。
AlexJamesBrown.JoeBlogs.Structs.Post m = new AlexJamesBrown.JoeBlogs.Structs.Post(); // 实例化这个类来存储博客内容。
m.description = txt_content.Text;   //博客的正文。
m.title = txt_title.Text.Trim();  //博客的标题。
m.categories = new string[] { "seo" };  //博客的分类,标签,163的貌似一定要给这个赋值,不然会出错。
m.dateCreated = System.DateTime.Now;  //博客的时间
var userBlogs = mw.NewPost(m, true);  //发送博客,并返回一些数据。
 

界面截图:

using:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using AlexJamesBrown.JoeBlogs; //调用dll
using System.Threading;

需要用到的变量:

private string[,] userInfo=new string[200,3]; //保存用户所属网站,用户名,用户密码的2维数组。
private int userNum = -1; //保存配置从文件读取用户的总数。
private int finshedNum = -1; //保存已经发布成功博客的数目。

读取配置文件,根据是否勾选来判断是否要读取响应网站的账号:

//读取配置文件。
private void btn_readIni_Click(object sender, EventArgs e)
{
List
<CheckBox> ckbs = new List<CheckBox> { ckb_cnblogs,ckb_csdn,ckb_sina,ckb_sohu,ckb_163};
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
txt_filePath.Text
= openFileDialog1.FileName;
FileStream fs
= new FileStream(txt_filePath.Text, FileMode.Open, FileAccess.Read);
StreamReader sr
= new StreamReader(fs);
int y = -1;
for (int x = 0; x < 200; x++)
{
string str = sr.ReadLine();
if (str == null)
break;
if (str == "")
continue;
string[] strArr = str.Split('&');
var tmpLabel
= ckbs.Find(Text =>
{
if (Text.Text.Trim() == strArr[0].Trim())
return true;
else
return false;
}
);
if (tmpLabel.Checked)
{
y
++;
userInfo[y,
0] = strArr[0].Trim();
userInfo[y,
1] = strArr[1].Trim();
userInfo[y,
2] = strArr[2].Trim();
userNum
++;
}
}
sr.Close();
fs.Close();
lb_status.Text
= "配置文件读取完成。";
}
catch (Exception ex)
{
MessageBox.Show(
"配置文件格式有问题!"+ex.Message);
}
}
}

发布博客,为了提高效率,每个账号发表博客都是单独一个线程,相互不影响:

//发表博客。
private void btn_send_Click(object sender, EventArgs e)
{
for (int x = 0; x <= userNum; x++)
{
btn_readIni.Enabled
= false;
btn_send.Enabled
= false;
userObj tmpObj
= new userObj();
tmpObj.webSite
= userInfo[x, 0];
tmpObj.username
= userInfo[x, 1];
tmpObj.passwd
= userInfo[x, 2];
Thread sendBlogsThread
= new Thread(new ParameterizedThreadStart(sendBlogs)); //每个账号启用一个线程。
sendBlogsThread.Start(tmpObj);
}
}

//
private void sendBlogs(object obj)
{
Thread.CurrentThread.IsBackground
= true;
Control.CheckForIllegalCrossThreadCalls
= false;
userObj user
= (userObj)obj;
string webSite = user.webSite;
string username = user.username;
string passwd = user.passwd;
string url = "" ;
switch (webSite)
{
case "sina":
url
="http://upload.move.blog.sina.com.cn/blog_rebuild/blog/xmlrpc.php".Trim();
break;
case "sohu":
break;
case "csdn":
url
= "http://blog.csdn.net/"+username+"/services/metablogapi.aspx";
break;
case "cnblogs":
url
= "http://"+username+".cnblogs.com/services/metaweblog.aspx".Trim();
break;
case "163":
url
= "http://os.blog.163.com/api/xmlrpc/metaweblog/".Trim();
break;
}
try
{
MetaWeblogWrapper mw
= new MetaWeblogWrapper(url, username, passwd);
AlexJamesBrown.JoeBlogs.Structs.Post m
= new AlexJamesBrown.JoeBlogs.Structs.Post();
m.description
= txt_content.Text;
m.title
= txt_title.Text.Trim();
m.categories
= new string[] { "seo" };
m.dateCreated
= System.DateTime.Now;
var userBlogs
= mw.NewPost(m, true);
finshedNum
++;
lb_status.Text
= finshedNum.ToString() + "/" + userNum.ToString();
if (finshedNum == userNum)
{
btn_readIni.Enabled
= true;
btn_send.Enabled
= true;
lb_status.Text
= lb_status.Text + " 上传完成!";
}
}
catch (Exception ex)
{
MessageBox.Show(
"调用博客接口出错:"+ex.Message);
}
}
}

//用于启动时线程传参。
class userObj
{
public string webSite;
public string username;
public string passwd;
}

作者: FMN 发表于 2011-05-27 23:42 原文链接

推荐.NET配套的通用数据层ORM框架:CYQ.Data 通用数据层框架
新浪微博粉丝精灵,刷粉丝、刷评论、刷转发、企业商家微博营销必备工具"