基于Http Basic Authorization实现的9911miniblog推送
关键字:Http Header,Authorization,Basic64,OpenApi,HttpWatch
小弟我做了一个个人站(www.gaotianpu.com) ,打算实现一个功能,
当有新的blog发布时,推送一条内容更新通知到9911.com ,这样可以增加小站的知名度嘛,哈哈。
查阅了该api的文档,发现“如需要验证用户,验证方式为HTTP Basic 验证”。
为此,了解了一下如何实现这个http basic验证。
第一步,用HttpWatch查看请求结构
访问:http://api.9911.com/statuses/update.xml,会弹出windows登录框,提示输入账号密码。
输入9911的账号+密码,
HttpWatch截图,通过分析,需要再http request head里设置Authorization的属性
第二步,用C#实现请求
public class MySpace9911
{
private System.Net.WebClient client = new System.Net.WebClient();
private MySpace9911() { }
public MySpace9911(string uname, string pwd)
{
string auth = Souout.Utility.ConvertEx.ToBase64String(string.Format("{0}:{1}", uname, pwd));
client.Headers.Add("Authorization", "Basic " + auth);
}
public void PostMiniBlog(string content)
{
System.Collections.Specialized.NameValueCollection nvc = System.Collections.Specialized.new NameValueCollection();
nvc.Add("status", content);
client.UploadValues("http://api.9911.com/statuses/update.xml", "Post", nvc);
}
}
{
private System.Net.WebClient client = new System.Net.WebClient();
private MySpace9911() { }
public MySpace9911(string uname, string pwd)
{
string auth = Souout.Utility.ConvertEx.ToBase64String(string.Format("{0}:{1}", uname, pwd));
client.Headers.Add("Authorization", "Basic " + auth);
}
public void PostMiniBlog(string content)
{
System.Collections.Specialized.NameValueCollection nvc = System.Collections.Specialized.new NameValueCollection();
nvc.Add("status", content);
client.UploadValues("http://api.9911.com/statuses/update.xml", "Post", nvc);
}
}
ok,测试通过~,很简单啊
附:
9911是myspace.cn推出的miniblog网站,其api文档: