. Net code example for calling the Sina Weibo Open Platform Interface

Source: Internet
Author: User

To use the Sina Weibo Open Platform interface, you must first apply for an account. Application Method:To @ Weibo Open Platform Send a private email or send an email to your open_sina_mblog@vip.sina.com, attach your email, Weibo personal homepage, phone, and brief introduction.

After sending the application email, we received the application email in less than one hour. Then go to the Sina Weibo open platform to view relevant documents.Basic auth for User Authentication) I found that the Sina Weibo Development Team recommended a blog post written by Q. Lee. Lulu in the garden: the implementation of various languages for accessing resources requiring HTTP Basic Authentication authentication. This articleArticleThis is an important reference for us. However, this article only addresses the situation of "get" requests, while the interfaces on Sina Weibo Open Platform require the HTTP Request Method to be "Post ", we also refer to another blog written by wusheng Yutang in the garden: Use httpwebrequest to send a custom POST request. Thank you for sharing these two friends!

Next, let's start the. NET call to the Sina Weibo Open Platform interface.

1. First, we need to obtain an app key. If we create an app in "My applications" on the Sina Weibo open platform, the app key is generated, for example, 123456.

2. Find the API you want to call in the Sina Weibo API document. Here we assume that the Weibo API-statuses/update is called, and the URL is disabled. The appkey is the previously obtained app key.

3. Prepare data

1) prepare user verification data:

    String  Username  =     "  T@cnblogs.com  "  ;
String Password = " Cnblogs.com " ;
String Usernamepassword = Username + " : " + Password;

Username is your Weibo login username and password is your blog password.

2) Prepare the called URL and the data to be post:

    String  URL  =     "  Http://api.t.sina.com.cn/statuses/update.json  "  ;
String News_title = " Vs2010 online series: AboutProgramMembers' love stories " ;
Int News_id = 62747 ;
String T_news = String . Format ( " {0}, http://news.cnblogs.com/n/5111 }/ " , News_title, news_id   );
String Data = " Source = 123456 & status = " + System. Web. httputility. urlencode (t_news );

4. Prepare the httpwebrequest object used to initiate the request:

System. net. webrequest=System. net. webrequest. Create (URL );
System. net. httpwebrequest httprequest=WebrequestAsSystem. net. httpwebrequest;

5. Prepare creden for user authentication:

 System. net. credentialcache mycache  =     New  System. net. credentialcache ();
Mycache. Add ( New Uri (URL ), " Basic " , New System. net. networkcredential (username, password ));
Httprequest. Credentials = Mycache;
Httprequest. headers. Add ( " Authorization " , " Basic " +
Convert. tobase64string ( New System. Text. asciiencoding (). getbytes (usernamepassword )));

6. Initiate a POST request:

 Httprequest. Method  =     "  Post  "  ;
Httprequest. contenttype = " Application/X-WWW-form-urlencoded " ;
System. Text. Encoding = System. Text. encoding. ASCII;
Byte [] Bytestopost = Encoding. getbytes (data );
Httprequest. contentlength = Bytestopost. length;
System. Io. Stream requeststream = Httprequest. getrequeststream ();
Requeststream. Write (bytestopost, 0 , Bytestopost. Length );
Requeststream. Close ();

AboveCodeAfter successful execution, the content will be sent to your Weibo account.

7. Get the response content from the server:

 
System. net. webresponse WR=Httprequest. getresponse ();
System. Io. Stream receivestream=Wr. getresponsestream ();
Using(System. Io. streamreader Reader= NewSystem. Io. streamreader (receivestream, system. Text. encoding. utf8 ))
{
StringResponsecontent=Reader. readtoend ();
}

Now, the. NET call to the Sina Weibo Open Platform interface is complete. It's easy.

If no Q. lee. the article by Lulu and wusheng fish soup is not that easy for us. This may be the value of sharing, your little experience may be of great help to others.

So, let's share it with you. Although it's not much experience, it's just an arrangement, but it may be helpful to people in need.
Forwarded from: http://blog.163.com/da7_1@126/blog/static/10407267820107204730339/

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.