下一篇:博客小工具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
