如何使用.NET调用新浪微博接口的实例
来源:互联网 发布:出售博客源码违法吗 编辑:程序博客网 时间:2024/05/19 03:43
本文详细讲下.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();}好了,.NET调用新浪微博开放平台接口之旅就完成
- 如何使用.NET调用新浪微博接口的实例
- NET调用新浪微博接口示例
- 使用 httpClient 调用新浪微博接口
- 新浪微博发布一条微博和图片的接口调用实例
- .NET调用新浪微博开放平台接口的代码示例
- .NET调用新浪微博开放平台接口的代码示例
- .NET调用新浪微博开放平台接口的代码示例
- ASP.NET调用新浪微博开放平台API接口的代码示例
- .NET调用新浪微博开放平台接口的代码示例
- ASP.NET 调用 新浪微博开放平台API 接口的操作
- .NET调用新浪微博开放平台接口的代码示例
- ASP.NET调用新浪微博开放平台API接口的代码示例
- 调用新浪微博开放平台接口
- Python如何调用新浪api接口的问题
- 如何使用新浪微博的sdk
- 新浪微博API使用之python接口的使用
- 如何通过python调用新浪微博的API
- 如何通过python调用新浪微博的API
- java中synchronized用法
- LineSeriesGroup: Flickering on resize?
- C#中运用SQLDMO备份和恢复SQLServer数据库
- rpm2cpio和cpio
- 类模板之队列
- 如何使用.NET调用新浪微博接口的实例
- 更改jboss默认使用的rmi端口,避免冲突
- java IO学习笔记
- 关于Object-C的NSAutoreleasePool的学习
- 字符串处理函数
- Silverlight 非模态的悬浮窗口
- 软件测试环境、平台和工具
- 探索 Pexpect
- 触摸,事件,UIGestureRecognizer