实例介绍
【实例简介】想抓取的话 需在 新浪开放平台申请appid appsecret等信息,否则可能会提示没权限
主要参考价值是:控制台(客户端)程序 打开 浏览器 并读取浏览器中的 url参数 code
【实例截图】
【核心代码】
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.IO; using System.Threading; using System.Diagnostics; using NetDimension.OpenPlatform; using System.Windows.Forms; using SHDocVw; using Microsoft.Win32; namespace TestConsoleApplication { class Program { private static bool first = true; private static bool firstEnter = true; private static Thread _th = null; static void Main(string[] args) { Console.Title = "新浪微博采集器"; Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); //Console.WriteLine("开放平台 for C# SDK DEMO"); //Console.WriteLine("版权所有 (C) 新浪网云南频道林选臣 1997-2012 保留所有权利。"); Console.WriteLine(); //ResetIEDefaultBrowser(); while (true) { _th = new Thread(SDKTest); Thread.Sleep(5 * 1000); _th.Start(); if (firstEnter) { //Console.Write("再来一次?[y/n]"); Console.Write("请先登录微博,在ie浏览器url上出现code时,按回车开始……"); if (Console.ReadKey().Key != ConsoleKey.Enter) { break; } Console.WriteLine(); firstEnter = false; } } } private static string key = ""; private static string secret = ""; private static string callback = ""; private static bool isSina = true; private static void SDKTest() { if (first) { frmSettings SettingsDialog = new frmSettings(); if (SettingsDialog.ShowDialog() != System.Windows.Forms.DialogResult.OK) return; key = SettingsDialog.txtKey.Text; secret = SettingsDialog.txtSecret.Text; callback = SettingsDialog.txtCallback.Text; isSina = SettingsDialog.rbSina.Checked; first = false; } OpenPlatformClient Client = null; if (isSina) { var sina = new SinaClient(key, secret, callback); Process.Start(sina.GetAuthorizeUrl()); string code = ""; SHDocVw.ShellWindows shellwindows = new SHDocVw.ShellWindows(); foreach (InternetExplorer ie in shellwindows) { string filename = Path.GetFileNameWithoutExtension(ie.FullName).ToLower(); if (filename.Equals("iexplore")) { string str = ie.LocationURL.ToString(); if (str.IndexOf("?code") > 0) { code = str.Split('=')[1].ToString(); break; } //else SDKTest(); } } if (code != "") { try { Console.Write("输入Code:"); Console.Write(code); sina.GetAccessTokenByAuthorizationCode(code); Client = sina; //Get例子 var result = Client.SendCommand("statuses/home_timelinre"); Console.WriteLine(result); //Console.WriteLine(); ////发微博测试 //result = Client.SendCommand("statuses/update", RequestMethod.Post, // new RequestParameter("status",string.Format("我累个擦!发表于{0}",DateTime.Now))); ////发图片测试 //MemoryStream ms = new MemoryStream(); //Properties.Resource.Example.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); //result = Client.SendCommand("statuses/upload", RequestMethod.Post, // new RequestParameter("status", string.Format("我累个擦!传个图片。发表于{0}", DateTime.Now)), // new RequestParameter("pic", ms.GetBuffer())); //ms.Close(); } catch(Exception ex) { Log.LogMsg(ex.ToString()); } } } else { var qq = new TencentClient(key, secret, callback); Process.Start(qq.GetAuthorizeUrl()); Console.Write("输入Code:"); var code = Console.ReadLine(); Console.Write("输入OpenID:"); var openid = Console.ReadLine(); qq.GetAccessTokenByAuthorizationCode(code); qq.OpenID = openid; Client = qq; //Get例子 var result = Client.SendCommand("statuses/home_timeline"); Console.WriteLine(result); Console.WriteLine(); //发微博测试 result = Client.SendCommand("t/add", RequestMethod.Post, new RequestParameter("content", string.Format("我累个擦!发表于{0}", DateTime.Now))); //发图片测试 MemoryStream ms = new MemoryStream(); Properties.Resource.Example.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); result = Client.SendCommand("t/add_pic", RequestMethod.Post, new RequestParameter("content", string.Format("我累个擦!传个图片。发表于{0}", DateTime.Now)), new RequestParameter("pic", ms.GetBuffer())); ms.Close(); } } //public static void UpdateSql(string sql) //{ // string strConn = System.Configuration.ConfigurationManager.ConnectionStrings["conn"].ToString(); // SqlConnection conn = new SqlConnection(); // conn.ConnectionString = strConn; // try // { // conn.Open(); // SqlCommand cmd = new SqlCommand(sql, conn); // cmd.ExecuteNonQuery(); // } // catch (Exception ex) // { // throw new Exception(ex.Message); // } // conn.Close(); //} private static string TransDate(string strDate) { System.Globalization.CultureInfo cultureInfo = System.Globalization.CultureInfo.CreateSpecificCulture("en-US"); string format = "ddd MMM d HH:mm:ss zz00 yyyy"; DateTime dt = DateTime.ParseExact(strDate, format, cultureInfo); // 将字符串转换成日期 return dt.ToString("yyyy-MM-dd HH:mm:ss"); } public static bool ResetIEDefaultBrowser() { string mainKey = @"http\shell\open\command"; string nameKey = @"http\shell\open\ddeexec\Application"; string IEPath = @"C:\Program Files\Internet Explorer\iexplore.exe"; bool result = false; try { string value = string.Format("\"{0}\" -- \"%1\"", IEPath); RegistryKey regKey = Registry.ClassesRoot.OpenSubKey(mainKey, true); regKey.SetValue("", value); regKey.Close(); regKey = Registry.ClassesRoot.OpenSubKey(nameKey, true); regKey.SetValue("", "IExplore"); regKey.Close(); result = true; } catch { } return result; } } }
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论