原文:
我也是个新手,也是第一次使用开放平台做开发,刚开始感到无处下手,经过半天的摸索终于成功的调用了API,现在把这点经验介绍给新手(高手就没必要看了,当然,如果你能提些意见和建议,我感激不尽),愿同大家一起交流。
本例介绍的是如何用API提交数据(发布一条微博)和用API获取数据(获取最新更新的20条公共微博消息),也就是官方API中的“获取下行数据集(timeline)接口”下的“statuses/public_timeline 获取最新更新的公共微博消息”和“微博访问接口”下的“statuses/update 发布一条微博信息”。
首先你要有一个新浪微博帐号,还要申请一个app key(具体请参考然后在VS中新建一个解决方案,在解决方案中添加一个类库和一个网站,并添加引用(网站引用类库)。
由于发布微博是POST请求,获取数据是GET请求,且通过HTTP普通验证(Basic Authentication)方式授权,因此我把这些功能写在一个类中(放在类库中),代码如下(这个类参考了没有仔细考虑是否达到了通用):
发送请求及授权代码
using System;
using System.Net;
using System.IO;
namespace SinaMiniBlogLib
{
public class SendRequest
{
string username;
string password;
string key;
public string UsernamePassword
{
get { return username + ":" + password; }
}
#region 构造函数
public SendRequest()
{
username = "你的新浪微博帐号";
password = "你的新浪微博密码";
key = "你申请的app key";
}
public SendRequest(string username, string password,string key)
{
this.username = username;
this.password = password;
this.key = key;
}
#endregion
#region 发送GET请求
///
/// 发送GET请求
///
///
地址
/// string
public string SendGetRequest(string url)
{
string Content=string.Empty;
//准备用于发起请求的HttpWebRequest对象:
WebRequest webRequest = WebRequest.Create(url+"?source="+key);
HttpWebRequest httpRequest = webRequest as HttpWebRequest;
//准备用于用户验证的凭据
CredentialCache myCache = new CredentialCache();
myCache.Add(new Uri(url), "Basic", new NetworkCredential(username, password));
httpRequest.Credentials = myCache;
httpRequest.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new System.Text.ASCIIEncoding().GetBytes(UsernamePassword)));
//GET方法
httpRequest.Method = "GET";
HttpWebResponse httpRespone = (HttpWebResponse)httpRequest.GetResponse();
if (httpRespone != null httpRespone.StatusCode == HttpStatusCode.OK)
{
using (StreamReader sr = new StreamReader(httpRespone.GetResponseStream()))
{
Content = sr.ReadToEnd();
}
}
return Content;
}
#endregion
#region 发送POST请求
///
/// 发送POST请求
///
///
网址
///
内容
/// string
public string SendPostRequest(string url,string Content)
{
string result = string.Empty;
//准备调用的URL及需要POST的数据:
string data = "source="+key+"status=" + Content;
//准备用于发起请求的HttpWebRequest对象:
WebRequest webRequest = WebRequest.Create(url);
HttpWebRequest httpRequest = webRequest as HttpWebRequest;
//准备用于用户验证的凭据
CredentialCache myCache = new CredentialCache();
myCache.Add(new Uri(url), "Basic", new NetworkCredential(username, password));
httpRequest.Credentials = myCache;
httpRequest.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new System.Text.ASCIIEncoding().GetBytes(UsernamePassword)));
//发起POST请求
httpRequest.Method = "POST";
httpRequest.ContentType = "application/x-www-form-urlencoded";
System.Text.Encoding encoding = System.Text.Encoding.ASCII;
byte【】 bytesToPost = encoding.GetBytes(data);
httpRequest.ContentLength = bytesToPost.Length;
System.IO.Stream requestStream = httpRequest.GetRequestStream();
requestStream.Write(bytesToPost, 0, bytesToPost.Length);
requestStream.Close();
//获取服务端的响应内容
System.Net.WebResponse wr = httpRequest.GetResponse();
System.IO.Stream receiveStream = wr.GetResponseStream();
using (System.IO.StreamReader reader = new System.IO.StreamReader(receiveStream, System.Text.Encoding.UTF8))
{
result = reader.ReadToEnd();
}
return result;
}//代码效果参考:http://www.ezhiqi.com/zx/art_3184.html
#endregion
}
}
然后在类库中新建两个实体类status和user,字段与官方API中一致:
status实体类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SinaMiniBlogLib
{
public class status
{
public status()
{
user = new user();
}
#region 属性
private DateTime created_at;
private string id;
private string text;
private string source;
pri