ASP.NET 调用 新浪微博开放平台API 接口的操作
2013-05-13 11:58
1166 查看
使用新浪微博开放平台接口,需要先申请一帐号,申请方法:给 @微博开放平台 发送私信,或者给open_sina_mblog@vip.sina.com发邮件,附上您的email,微博个人主页,电话,和简单介绍。
我们发了申请邮件后,不到1小时就收到了申请通过的邮件。然后进入新浪微博开放平台查看相关文档,在文档中(使用Basic Auth进行用户验证)发现新浪微博开发团队推荐了园子里的Q.Lee.lulu写的一篇博文:访问需要HTTP Basic Authentication认证的资源的各种语言的实现。这篇文章成为了我们的重要参考,但该文只是针对“GET”请求的情况,而新浪微博开放平台接口要求HTTP请求方式为“POST”,我们又参考了园子里的乌生鱼汤写的另一篇博文:
使用HttpWebRequest发送自定义POST请求。在这里感谢两位园友的分享!
接下来,我们开始.NET调用新浪微博开放平台接口之旅。
1. 首先我们要获取一个App Key,在新浪微博开放平台的“我的应用”中创建一个应用,就会生成App Key,假设是123456。
2. 在新浪微博API文档中找到你想调用的API,这里我们假定调用发表微博的API-statuses/update,url是http://api.t.sina.com.cn/statuses/update.json,POST的参数:source=appkey&status=微博内容。其中appkey就是之前获取的App
Key。
3. 准备数据
1) 准备用户验证数据:
string username = "t@cnblogs.com";
string password = "cnblogs.com";
string usernamePassword = username + ":" + password;
username是你的微博登录用户名,password是你的博客密码。
2) 准备调用的URL及需要POST的数据:
string url = "http://api.t.sina.com.cn/statuses/update.json";
string news_title = "VS2010网剧合集:讲述程序员的爱情故事";
int news_id = 62747;
string t_news = string.Format("{0},http://news.cnblogs.com/n/{1}/", news_title, news_id );
string data = "source=123456&status=" + System.Web.HttpUtility.UrlEncode(t_news);
4. 准备用于发起请求的HttpWebRequest对象:
System.Net.WebRequest webRequest = System.Net.WebRequest.Create(url);
System.Net.HttpWebRequest httpRequest = webRequest as System.Net.HttpWebRequest;
5. 准备用于用户验证的凭据:
System.Net.CredentialCache myCache = new System.Net.CredentialCache();
myCache.Add(new Uri(url), "Basic", new System.Net.NetworkCredential(username, password));
httpRequest.Credentials = myCache;
httpRequest.Headers.Add("Authorization", "Basic " +
Convert.ToBase64String(new System.Text.ASCIIEncoding().GetBytes(usernamePassword)));
6. 发起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();
7. 获取服务端的响应内容: 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)
{
string responseContent = reader.ReadToEnd();
} 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)
{
string responseContent = reader.ReadToEnd();
}
.NET调用新浪微博开放平台接口就完成了!!
我们发了申请邮件后,不到1小时就收到了申请通过的邮件。然后进入新浪微博开放平台查看相关文档,在文档中(使用Basic Auth进行用户验证)发现新浪微博开发团队推荐了园子里的Q.Lee.lulu写的一篇博文:访问需要HTTP Basic Authentication认证的资源的各种语言的实现。这篇文章成为了我们的重要参考,但该文只是针对“GET”请求的情况,而新浪微博开放平台接口要求HTTP请求方式为“POST”,我们又参考了园子里的乌生鱼汤写的另一篇博文:
使用HttpWebRequest发送自定义POST请求。在这里感谢两位园友的分享!
接下来,我们开始.NET调用新浪微博开放平台接口之旅。
1. 首先我们要获取一个App Key,在新浪微博开放平台的“我的应用”中创建一个应用,就会生成App Key,假设是123456。
2. 在新浪微博API文档中找到你想调用的API,这里我们假定调用发表微博的API-statuses/update,url是http://api.t.sina.com.cn/statuses/update.json,POST的参数:source=appkey&status=微博内容。其中appkey就是之前获取的App
Key。
3. 准备数据
1) 准备用户验证数据:
string username = "t@cnblogs.com";
string password = "cnblogs.com";
string usernamePassword = username + ":" + password;
username是你的微博登录用户名,password是你的博客密码。
2) 准备调用的URL及需要POST的数据:
string url = "http://api.t.sina.com.cn/statuses/update.json";
string news_title = "VS2010网剧合集:讲述程序员的爱情故事";
int news_id = 62747;
string t_news = string.Format("{0},http://news.cnblogs.com/n/{1}/", news_title, news_id );
string data = "source=123456&status=" + System.Web.HttpUtility.UrlEncode(t_news);
4. 准备用于发起请求的HttpWebRequest对象:
System.Net.WebRequest webRequest = System.Net.WebRequest.Create(url);
System.Net.HttpWebRequest httpRequest = webRequest as System.Net.HttpWebRequest;
5. 准备用于用户验证的凭据:
System.Net.CredentialCache myCache = new System.Net.CredentialCache();
myCache.Add(new Uri(url), "Basic", new System.Net.NetworkCredential(username, password));
httpRequest.Credentials = myCache;
httpRequest.Headers.Add("Authorization", "Basic " +
Convert.ToBase64String(new System.Text.ASCIIEncoding().GetBytes(usernamePassword)));
6. 发起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();
7. 获取服务端的响应内容: 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)
{
string responseContent = reader.ReadToEnd();
} 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)
{
string responseContent = reader.ReadToEnd();
}
.NET调用新浪微博开放平台接口就完成了!!
相关文章推荐
- asp.net 模拟CURL调用微信公共平台API 上传下载多媒体文件接口
- 用VC++2005调用用ASP.NET(C#)做的WebSerice接口进行操作数据库
- ASP.NET后台手动调用POST提交并获取返回值 用于短信平台接口
- asp.net 模拟CURL调用微信公共平台API 上传下载多媒体文
- [原创]ASP.NET MVC调用美图秀秀开放平台拼图实现
- asp.net mvc短信接口调用——阿里大于API开发心得
- [原创]ASP.NET MVC调用美图秀秀开放平台拼图实现
- Winform实现调用asp.net数据接口实例
- ASP.NET MVC API 接口验证的示例代码
- ASP.NET Web API与Owin OAuth:使用Access Toke调用受保护的API
- 微信开放平台 公众号第三方平台开发 教程四 代公众号调用接口的SDK和demo
- asp.net 前台调用后操作数据库方法
- Asp.Net大型项目实践(5)-用Unity实现层与层之间的接口调用(附源码)
- 浅谈ASP.NET调用淘宝API之初入江湖
- Winform项目调用asp.net数据接口
- ASP.NET Core 中文文档 第三章 原理(16).NET开放Web接口(OWIN)
- 通过调用多个操作创建 ASP.NET MVC 视图
- 微信开放平台 公众号第三方平台开发 教程四 代公众号调用接口的SDK和demo
- ASP.NET Web API与Owin OAuth:使用Access Toke调用受保护的API(二)
- PHP学习之[第11讲]新浪微博开放平台 PHP 与 OAuth 接口(1)