Sina Weibo PC Client (DOTNET winform C #, C # Call Sina Weibo API code, source code download) -- Preliminary Research (second part of the built-in link)

Source: Internet
Author: User
Tags dotnet truncated visual studio 2010

Article 2: Sina Weibo PC Client (DOTNET winform)-Introduction to function implementation

 

C # source code download

 

Recently, I tried to use DOTNET technology to implement the Sina Weibo PC client. Over the past few days, I have implemented login, Weibo list, and publish the text Weibo function. It is basically no difficulty to call Sina API, I have been annoyed with the processing of Weibo list forms for a while. Windows form is not used much, and this development is also quite slow.
 
Environment:
Operating System: Windows 7 Ultimate
Integrated Development Environment: Visual Studio 2010 ultimate
. NET Framework: 3.5
 
Let's take a look at it first:
1. logon Interface
 
2. log on
 
3. Load the main interface for the first time
 
4. Home Page
 
5. Flip pages
 
6. If there are images in the blog, click the thumbnail to view the larger image.
 
Sina Weibo APIs return two types of data: XML and JSON. When I log on, I directly use the XML data read by dataset to store it in a static user information class; while the blog list data I read is in JSON format, deserializes JSON data into a collection of generic objects (classes). The classes here are nested classes.
 
Then, draw a list to show that all the data controls are dynamically added. The most annoying thing here is the location, this may also be the cause of slow loading of 20 data entries per page (20 entries are returned for one page by default). It may take up to 2 seconds, usually 4-6 seconds, and the download will be provided later, you can try it. In addition, the reasons may be: 1) network speed; 2) output images, some directly output large images. If there are images in the reply source Weibo, the thumbnails will not always be obtained, we had to judge the small graph, the middle graph, and the big graph in sequence, and which output was the first. The line spacing of text displayed with label cannot be adjusted, so we had to make a custom control, which increases the line spacing based on the font size.
 
Although the interface uses the skin (SSK), it looks much better than the original window form interface, but there is still a gap between the air than Adobe, maybe you can try to use WPF.
 
Functions to be implemented in the future (some functions do not necessarily provide APIs before all of them are viewed ):
1) Forward, add to favorites, and reply
2) You can use emojis to post and upload images.
3) follow and fans list
4) view your own Weibo and others' Weibo lists
5) Personal Configuration Information
6) content on the right of the personal homepage after I log on to Sina Weibo on the left side of the main interface
......
The workload is quite large.
 
Currently, after processing, a range of 4m-25m is occupied after the program runs. If not processed, it will keep rising, 100 m, 200 m ,.......
During page turning, the load display instantaneous CPU usage can reach about 30%, which is generally relatively low and below 5%.
During screen scrolling, the CPU usage is generally less than 18%, Which is instantaneous.
We can see that asynchronous data loading uses multiple threads, mainly using backgroundworker.
 
In addition to the above problems, the scrolling content is a little flash while scrolling, and no good solution is found.
 
You are welcome to discuss with others and give your valuable comments. Experienced people provide guidance :).
 
The code for reading the Sina Weibo API is attached:

Using system; <br/> using system. collections. generic; <br/> using system. text; <br/> using system. io; <br/> using system.. Net; <br/> using mbclient. entities; <br/> using system. runtime. serialization. JSON; <br/> using system. data; <br/> using system. drawing; <br/> namespace mbclient. API <br/>{< br/> internal class readdatabysinaapi <br/>{< br/> internal readdatabysinaapi () {}< br/> // <summary> <br/> // Root Log on to and read the user information to dataset <br/> // if the user's Sina pass identity authentication is successful and the user has activated Weibo, the HTTP status is 200; <br/> // If not, the system returns the status and error message 401. This method uses to determine whether a user's identity is valid and has already activated Weibo. <Br/> // http://open.t.sina.com.cn/wiki/index.php/Account/verify_credentials <br/> // </Summary> <br/> // <Param name = "url"> api url (XML format) http://api.t.sina.com.cn/account/verify_credentials.xml? Source = appkey <br/> /// </param> <br/> // <returns> </returns> <br/> internal static dataset readxmldatatodataset (string URL) <br/>{< br/> credentialcache mycache = new credentialcache (); <br/> mycache. add (New uri (URL), "Basic", new networkcredential (userinfo. username, userinfo. password); <br/> webrequest myreq = webrequest. create (URL); <br/> myreq. credentials = mycache; <br/> myreq. headers. add (" Authorization "," Basic "+ convert. tobase64string (New asciiencoding (). getbytes (userinfo. usernamepassword); <br/> webresponse wR = myreq. getresponse (); <br/> dataset DS = new dataset (); <br/> using (Stream receivestream = Wr. getresponsestream () <br/>{< br/> streamreader reader = new streamreader (receivestream, encoding. utf8); <br/> // string content = reader. readtoend (); <br/> DS. readxml (Reader); <Br/> reader. close (); <br/>}< br/> Wr. close (); <br/> return Ds; <br/>}< br/> internal static string readxmldatatostring (string URL) <br/>{< br/> credentialcache mycache = new credentialcache (); <br/> mycache. add (New uri (URL), "Basic", new networkcredential (userinfo. username, userinfo. password); <br/> webrequest myreq = webrequest. create (URL); <br/> myreq. credentials = mycache; <br/> myreq. he Aders. add ("Authorization", "Basic" + convert. tobase64string (New asciiencoding (). getbytes (userinfo. usernamepassword); <br/> webresponse wR = myreq. getresponse (); <br/> string strdata = NULL; <br/> using (Stream receivestream = Wr. getresponsestream () <br/>{< br/> streamreader reader = new streamreader (receivestream, encoding. utf8); <br/> strdata = reader. readtoend (); <br/> reader. close (); <br/>} <Br/> Wr. close (); <br/> return strdata; <br/>}< br/> /// <summary> <br/> // return the latest n Weibo messages of the user in JSON format. The returned content is the same as that on the "my homepage. <Br/> // http://open.t.sina.com.cn/wiki/index.php/Statuses/friends_timeline <br/> // </Summary> <br/> // <Param name = "url"> api url (JSON format) http://api.t.sina.com.cn/statuses/friends_timeline.json? Source = appkey <br/> /// </param> <br/> // <returns> </returns> <br/> internal static string readjsondatatostring (string URL) <br/>{< br/> credentialcache mycache = new credentialcache (); <br/> mycache. add (New uri (URL), "Basic", new networkcredential (userinfo. username, userinfo. password); <br/> webrequest myreq = webrequest. create (URL); <br/> myreq. credentials = mycache; <br/> myreq. headers. add ("Authorization", "Basic" + convert. tobase64string (New asciiencoding (). getbytes (userinfo. usernamepassword); <br/> webresponse wR = myreq. getresponse (); <br/> string content = NULL; <br/> using (Stream receivestream = Wr. getresponsestream () <br/>{< br/> streamreader reader = new streamreader (receivestream, encoding. utf8); <br/> content = reader. readtoend (); <br/> reader. close (); <br/>}< br/> Wr. close (); <br/> return content; <br/>}< br/> /// <summary> <br/> // deserializes a json string into a status collection object. <br/> // </Summary> <br/> // <Param name = "url"> same as readjsondatatostring () </param> <br/> // <returns> </returns> <br/> internal static list <status> deserializejsontolistobject (string URL) <br/>{< br/> List <status> listobj; <br/> using (memorystream stream = new memorystream ()) <br/>{< br/> datacontractjsonserializer SER = new datacontractjsonserializer (typeof (list <status>); <br/> streamwriter wR = new streamwriter (Stream ); <br/> string strjson = readjsondatatostring (URL); <br/> Wr. write (strjson); <br/> Wr. flush (); <br/> stream. position = 0; <br/> Object OBJ = Ser. readobject (Stream); <br/> listobj = (list <status>) OBJ; <br/> Wr. close (); <br/>}< br/> return listobj; <br/>}< br/> /// <summary> <br/> // obtain and output the image. <br/> /// </Summary> <br/> /// <Param name = "imgurl"> image URL </param> <br/> /// <returns> </returns> <br/> internal static image getavatarimage (string imgurl) <br/>{< br/> URI myuri = new uri (imgurl); <br/> webrequest = webrequest. create (myuri); <br/> webresponse = webrequest. getresponse (); <br/> bitmap myimage = new Bitmap (webresponse. getresponsestream (); <br/> return (image) myimage; <br/>}< br/> /// <summary> <br/> // post a microblog <br/> /// </Summary> <br/> /// <Param name = "url"> API address <br/> // http://api.t.sina.com.cn/statuses/update.json <br/> /// </param> <br/> // <param name = "data"> appkey and Weibo content <br/> // "Source = 123456 & status =" + system. web. httputility. urlencode (Weibo content ); <br/> /// </param> <br/> // <returns> </returns> <br/> internal static bool postblog (string URL, string data) <br/>{< br/> try <br/>{< br/> system. net. webrequest = system. net. webrequest. create (URL); <br/> system. net. httpwebrequest httprequest = webrequest as system. net. httpwebrequest; <br/> system. net. credentialcache mycache = new system. net. credentialcache (); <br/> mycache. add (New uri (URL), "Basic", <br/> new system. net. networkcredential (userinfo. username, userinfo. password); <br/> httprequest. credentials = mycache; <br/> httprequest. headers. add ("Authorization", "Basic" + convert. tobase64string (<br/> new system. text. asciiencoding (). getbytes (userinfo. username + ":" + <br/> userinfo. password); <br/> httprequest. method = "Post"; <br/> httprequest. contenttype = "application/X-WWW-form-urlencoded"; <br/> system. text. encoding encoding = system. text. encoding. utf8; // system. text. encoding. ASCII <br/> byte [] bytestopost = encoding. getbytes (data); // system. web. httputility. urlencode (data) <br/> httprequest. contentlength = bytestopost. length; <br/> system. io. stream requeststream = httprequest. getrequeststream (); <br/> requeststream. write (bytestopost, 0, bytestopost. length); <br/> requeststream. close (); <br/> return true; <br/>}< br/> catch <br/>{< br/> return false; <br/>}< br/>} 


Thanks to this article of blog Park, great reference value: http://www.cnblogs.com/cmt/archive/2010/05/13/1733904.html

Note: The appkey is built in. I applied for it. Each IP Address can call up to 350 APIs per hour.

PS: Don't do anything bad ......

 

The complete XML data of a blog post is included:

<Status> <br/> <created_at> wed Nov 24 21:24:04 + 0800 2010 </created_at> <br/> <ID> 3858478793 </ID> <br/> <text> good inference. // @ Alime with white gloves: This is one of the most widely used famous sayings by Luo Yonghao. Change it for him: No bird came to the world to hide its guns. Therefore, those who hold a gun in their hands and use the principle of "shooting birds out of the head" must understand that every bird has a dream, and you can't beat it until you die. </Text> <br/> <source> <br/> <a href = "http://t.sina.com.cn" mce_href = "http://t.sina.com.cn"> Sina Weibo </a> <br/> </ source> <br/> <favorited> false </favorited> <br/> <truncated> false </truncated> <br/> <GEO/> <br/> <in_reply_to_status_id> </strong> <br/> <in_reply_to_user_id> </in_reply_to_user_id> <br/> <strong> </in_reply_to_screen_name> <br/> <user> <br/> <ID> 1197161814 </ID> <Br/> <screen_name> Kai-fu Lee </screen_name> <br/> <Name> Kai-fu Lee </Name> <br/> <province> 11 </province> <br/> <city> 1000 </city> <br/> <location> Beijing </location> <br/> <description> CEO of innovation workshop </description> <br/> <URL> http://blog.sina.com.cn/kaifulee </URL> <br/> <profile_image_url> http://tp3.sinaimg.cn/1197161814/50/1290146312/1 </profile_image_url> <br/> <domain> kaifulee </domain> <br/> <gender> m </Gender> <br/> <followe Rs_count> 2384862 </followers_count> <br/> <friends_count> 132 </friends_count> <br/> <statuses_count> 1039 </statuses_count> <br/> <favourites_count> 2 </favourites_count> <br/> <created_at> Fri Aug 28 00:00:00 + 0800 2009 </created_at> <br/> <Following> false </following> <br/> <verified> true </verified> <br/> <allow_all_act_msg> false </allow_all_act_msg> <br/> <geo_enabled> true </geo_enabled> <br/> </user> <br /> <Retweeted_status> <br/> <created_at> wed Nov 24 17:35:07 + 0800 2010 </created_at> <br/> <ID> 3853748245 </ID> <br/> <text> hope that those who like to use the principle of "shooting birds out of the head" to teach young people, in addition, the Chinese people who think they are very mature can understand the fact one day that some birds come to the world to do what they should do, rather than to hide themselves." </Text> <br/> <source> <br/> <a href = "http://t.sina.com.cn" mce_href = "http://t.sina.com.cn"> Sina Weibo </a> <br/> </ source> <br/> <favorited> false </favorited> <br/> <truncated> false </truncated> <br/> <GEO/> <br/> <in_reply_to_status_id> </strong> <br/> <in_reply_to_user_id> </in_reply_to_user_id> <br/> <strong> </in_reply_to_screen_name> <br/> <user> <br/> <ID> 1672204023 </ID> <Br/> <screen_name> ran-W </screen_name> <br/> <Name> ran-W </Name> <br/> <province> 11 </province> <br/> <city> 5 </city> <br/> <location> Chaoyang District, Beijing </location> <br/> <description> it is better to know nothing than to know everything.. Slowly go through the unknown future. </Description> <br/> <URL> http://blog.sina.com.cn/wongr11 </URL> <br/> <profile_image_url> http://tp4.sinaimg.cn/1672204023/50/1290577674/0 </profile_image_url> <br/> <domain> wongr </domain> <br/> <gender> F </gender> <br/> <followers_count> 757 </followers_count> <br/> <friends_count> 178 </friends_count> <br/> <statuses_count> 521 </statuses_count> <br/> <favourites_count> 4 </favourites_count> <br/> <created_at> mon Dec 21 00:00:00 + 0800 2009 </created_at> <br /> <Following> false </following> <br/> <verified> false </verified> <br/> <allow_all_act_msg> false </allow_all_act_msg> <br/> <geo_enabled> true </geo_enabled> <br/> </user> <br/> </retweeted_status> <br/> </status>

Status is the blog post published by the blogger, and retweeted_status is the blog post replied or forwarded by the blogger. Sina API has a field description.

 

Download Weibo Client


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.