下一篇:博客小工具3
昨天发了一个博客转发小工具有朋友说“能一键转发到各主流媒体站上就更好了”。一开始我以为会能难,需要登录啊还有cookie的管理啊模拟post请求啊,乱七八糟一大堆。心想算啦,太累人,还不一定搞得定。后来心里总想着有没有什么简单的办法,就在网上查资料。最后皇天不负有心人让我找到了HttpClient。
ok,接着昨天的来。昨天的只能获取别人文章的内容复制到粘贴板。今天让它能一键发布。
首先需要解决的问题就是登录问题,不然可能是不能发布的。
登录博客园的代码
1 /// <summary> 2 /// 登录博客园 3 /// </summary> 4 /// <param name="username"></param> 5 /// <param name="password"></param> 6 /// <returns></returns> 7 public bool LoginCnblogs(string username = "", string password = "") 8 { 9 httpClient = new HttpClient(); 10 httpClient.MaxResponseContentBufferSize = 256000; 11 httpClient.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36"); 12 String url = "http://passport.cnblogs.com/login.aspx"; 13 HttpResponseMessage response = httpClient.GetAsync(new Uri(url)).Result; 14 String result = response.Content.ReadAsStringAsync().Result; 15 16 String __EVENTVALIDATION = new Regex("id=\"__EVENTVALIDATION\" value=\"(.*?)\"").Match(result).Groups[1].Value; 17 String __VIEWSTATE = new Regex("id=\"__VIEWSTATE\" value=\"(.*?)\"").Match(result).Groups[1].Value; 18 String LBD_VCID_c_login_logincaptcha = new Regex("id=\"LBD_VCID_c_login_logincaptcha\" value=\"(.*?)\"").Match(result).Groups[1].Value; 19 20 //开始登录 21 url = "http://passport.cnblogs.com/login.aspx"; 22 List<KeyValuePair<String, String>> paramList = new List<KeyValuePair<String, String>>(); 23 paramList.Add(new KeyValuePair<string, string>("__EVENTTARGET", "")); 24 paramList.Add(new KeyValuePair<string, string>("__EVENTARGUMENT", "")); 25 paramList.Add(new KeyValuePair<string, string>("__VIEWSTATE", __VIEWSTATE)); 26 paramList.Add(new KeyValuePair<string, string>("__VIEWSTATEGENERATOR", "C2EE9ABB")); 27 paramList.Add(new KeyValuePair<string, string>("__EVENTVALIDATION", __EVENTVALIDATION)); 28 paramList.Add(new KeyValuePair<string, string>("tbUserName", username)); 29 paramList.Add(new KeyValuePair<string, string>("tbPassword", password)); 30 paramList.Add(new KeyValuePair<string, string>("LBD_VCID_c_login_logincaptcha", LBD_VCID_c_login_logincaptcha)); 31 paramList.Add(new KeyValuePair<string, string>("LBD_BackWorkaround_c_login_logincaptcha", "1")); 32 //paramList.Add(new KeyValuePair<string, string>("CaptchaCodeTextBox", imgCode)); 33 paramList.Add(new KeyValuePair<string, string>("btnLogin", "登 录")); 34 paramList.Add(new KeyValuePair<string, string>("txtReturnUrl", "http://home.cnblogs.com/")); 35 response = httpClient.PostAsync(new Uri(url), new FormUrlEncodedContent(paramList)).Result; 36 result = response.Content.ReadAsStringAsync().Result; 37 38 if (result.Contains("用户登录")) 39 return false; 40 else if (result.Contains("返回博客园首页")) 41 return true; 42 else 43 return false; 44 }
然后就是发布的代码
1 /// <summary> 2 /// 发布转发文章 3 /// </summary> 4 /// <returns></returns> 5 public bool Release(string title = "", string content = "") 6 { 7 List<KeyValuePair<String, String>> paramList = new List<KeyValuePair<String, String>>(); 8 paramList.Add(new KeyValuePair<string, string>("__VIEWSTATE", @"")); 9 paramList.Add(new KeyValuePair<string, string>("__VIEWSTATEGENERATOR", "FE27D343")); 10 paramList.Add(new KeyValuePair<string, string>("Editor$Edit$txbTitle", title)); 11 paramList.Add(new KeyValuePair<string, string>("Editor$Edit$EditorBody", content)); 12 paramList.Add(new KeyValuePair<string, string>("Editor$Edit$Advanced$ckbPublished", "on")); 13 paramList.Add(new KeyValuePair<string, string>("Editor$Edit$Advanced$chkDisplayHomePage", "on")); 14 paramList.Add(new KeyValuePair<string, string>("Editor$Edit$Advanced$chkComments", "on")); 15 paramList.Add(new KeyValuePair<string, string>("Editor$Edit$Advanced$chkMainSyndication", "on")); 16 paramList.Add(new KeyValuePair<string, string>("Editor$Edit$Advanced$txbEntryName", "")); 17 paramList.Add(new KeyValuePair<string, string>("Editor$Edit$Advanced$txbExcerpt", "")); 18 paramList.Add(new KeyValuePair<string, string>("Editor$Edit$Advanced$txbTag", "")); 19 paramList.Add(new KeyValuePair<string, string>("Editor$Edit$Advanced$tbEnryPassword", "")); 20 paramList.Add(new KeyValuePair<string, string>("Editor$Edit$lkbPost", "发布")); 21 22 HttpResponseMessage response = httpClient.PostAsync(new Uri("http://i.cnblogs.com/EditPosts.aspx?opt=1"), new FormUrlEncodedContent(paramList)).Result; 23 String result = response.Content.ReadAsStringAsync().Result; 24 if (result.Contains("发布成功")) 25 return true; 26 else 27 return false; 28 29 }
关键代码就只有这么几句,没什么好讲的。我也不知其所以然。
不说了 上效果图。
转发成功。
说明:
因为我登录博客园没有要输入验证码的情况,所以我就没有做验证码的处理了。大家的应该也不用输验证码吧。
还有就是没有做其他主流博客直接的相互转发,只有博客园内的转发。同学们有兴趣可以在次基础上做修改,欢迎改得“面目全非”。
环境:
vs2013 .net4.5
功能:
支持博客园一键转发,密码保存。下次就可以不用输入密码了。但是没有做加密措施。同学们可以自己接着折腾。
好了~最后发源码了。如果能感兴趣,拿起您的小手点个赞。如果您要反对 求您给你理由。
下一篇:博客小工具3