微博客户端登陆
界面:
<Window x:Class="WpfApp1.weibo" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApp1" mc:Ignorable="d" Title="weibo" Height="450" Width="800"> <Grid Height="Auto"> <WebBrowser x:Name="MicroBlogWebBrowser" ScrollViewer.VerticalScrollBarVisibility ="Disabled" UseLayoutRounding="False" Grid.ColumnSpan="3" Navigating="MicroBlogWebBrowser_Navigating" LoadCompleted="MicroBlogWebBrowser_LoadCompleted" Loaded="MicroBlogWebBrowser_Loaded" Navigated="MicroBlogWebBrowser_Navigated"/> </Grid> </Window>
后台:
namespace WpfApp1 { /// <summary> /// weibo.xaml 的交互逻辑 /// </summary> public partial class weibo : Window { public weibo() { InitializeComponent(); MicroBlogWebBrowser.Navigate(new Uri("xxx")); } #region 属性 public string appid = "xx"; public string appsecret = "xxx"; public string redirecturl = "xxx"; #endregion /// <summary> /// 获取token /// </summary> /// <param name="code"></param> /// <returns></returns> protected WeiboAccessToken Get_token(string code) { var bodyData = new { client_id = appid, client_secret = appsecret, grant_type = "authorization_code", redirect_uri = redirecturl, code = code }; string strAccessToken = PostData.HttpPost($"https://api.weibo.com/oauth2/access_token?client_id={appid}&client_secret={appsecret}&grant_type=authorization_code&redirect_uri={redirecturl}&code={code}", JsonConvert.SerializeObject(bodyData)); if (string.IsNullOrEmpty(strAccessToken)) { return null; } WeiboAccessToken oauthToken = JSONHelper.ParseFromJson<WeiboAccessToken>(strAccessToken); return oauthToken; } private List<string> urlList = new List<string>(); private void MicroBlogWebBrowser_Navigating(object sender, System.Windows.Navigation.NavigatingCancelEventArgs e) { SetWebBrowserSilent(sender as WebBrowser, true); string url = e.Uri.ToString(); urlList.Add(url); string code = ""; if (url.Contains("?code")) { int iStart = url.IndexOf("="); int iEnd = url.Length; if (iStart < iEnd) { int codeLength = iEnd - iStart - 1; code = url.Substring(iStart + 1, codeLength); } } if (string.IsNullOrEmpty(code)) return; // Post获取Accesstoken 类 WeiboAccessToken accessToken = Get_token(code); if (accessToken == null) { return; } // 获取微博用户信息 WeiboUser weiUser = Get_WeiboUserInfo(accessToken.access_token, accessToken.uid); //不提示脚本错误 SetWebBrowserSilent(sender as WebBrowser, true); } private void MicroBlogWebBrowser_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e) { mshtml.HTMLDocument dom = (mshtml.HTMLDocument)MicroBlogWebBrowser.Document; //定义HTML dom.documentElement.style.overflow = "hidden"; //隐藏浏览器的滚动条 dom.body.setAttribute("scroll", "no"); //禁用浏览器的滚动条 SetWebBrowserSilent(sender as WebBrowser, true); } /// <summary> /// 调用Access_token获取用户信息 /// </summary> /// <param name="Access_token"></param> /// <param name="uid"></param> /// <returns></returns> protected WeiboUser Get_WeiboUserInfo(string Access_token, string uid) { string str = PostData.HttpGet("https://api.weibo.com/2/users/show.json?access_token=" + Access_token + "&uid=" + uid); WeiboUser weiUser = JSONHelper.ParseFromJson<WeiboUser>(str); return weiUser; } /// <summary> /// 获取Authorization Code, /// </summary> /// <returns></returns> protected string Get_AuthorizationCode() { //获取weibo跳转含有code的网址 string tempCode = ""; foreach (string url in urlList) { if (url.Contains("?code")) { tempCode = url; break; } } if (string.IsNullOrEmpty(tempCode)) return null; //qq最终获得的code string code = ""; int iStart = tempCode.IndexOf("="); int iEnd = tempCode.Length; if (iStart < iEnd) { int codeLength = iEnd - iStart - 1; code = tempCode.Substring(iStart + 1, codeLength); } return code; } private void MicroBlogWebBrowser_Loaded(object sender, RoutedEventArgs e) { } private void MicroBlogWebBrowser_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e) { } /// <summary> /// 设置浏览器静默,不弹错误提示框 /// </summary> /// <param name="webBrowser">要设置的WebBrowser控件浏览器</param> /// <param name="silent">是否静默</param> private void SetWebBrowserSilent(WebBrowser webBrowser, bool silent) { FieldInfo fi = typeof(WebBrowser).GetField("_axIWebBrowser2", BindingFlags.Instance | BindingFlags.NonPublic); if (fi != null) { object browser = fi.GetValue(webBrowser); if (browser != null) browser.GetType().InvokeMember("Silent", BindingFlags.SetProperty, null, browser, new object[] { silent }); } } /// <summary> /// TitleBar_MouseDown - Drag if single-click, resize if double-click /// </summary> private void TitleBar_MouseDown(object sender, MouseButtonEventArgs e) { if (e.ChangedButton == MouseButton.Left) if (e.ClickCount == 2) { //AdjustWindowSize(); } else { this.DragMove(); } } private void CloseButton_Click(object sender, RoutedEventArgs e) { this.Close(); } } /// <summary> /// 微博AccessToken类 /// </summary> public class WeiboAccessToken { public string access_token { get; set; } public string remind_in { get; set; } public int expires_in { get; set; } public string uid { get; set; } public string isRealName { get; set; } } /// <summary> /// 微博用户信息类 /// </summary> public class WeiboUser { public Int64 id { get; set; } public string idstr { get; set; } public string screen_name { get; set; } public string name { get; set; } public string province { get; set; } public string city { get; set; } public string location { get; set; } public string description { get; set; } public string url { get; set; } public string profile_image_url { get; set; } public string profile_url { get; set; } public string gender { get; set; } } }
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术