Realize the conversion between the microblogging address URL (ID) and mid

Source: Internet
Author: User
Tags integer json tostring

After publishing microblogging via the Sina Weibo API interface, returns a two-dimensional array, the returned array contains the content of the microblog, publishes the user information of the microblog, and so on. There is no URL to a successful microblogging post. Many times if you want to record information in a database, the microblogging URL should be essential. Search through Sina Weibo forum , the following methods were found.

Principle:

Sina Weibo's URL is like: Http://weibo.com/2480531040/z8ElgBLeQ such three parts.

The first part (green part) is the domain name of Sina Weibo, the second part (red part) is the main UID, the third part (blue) is a string of seemingly random strings.

It would be much better to compute the corresponding relationship between the blue string and the returned array by means of a method.

First group the blue string, from the back 4 characters a set, get the following three groups of characters:

Z

8Elg

Bleq

The numbers that convert them to 62 are 35, 2061702, 8999724, and they're grouped together as a bunch of 3520617028999724 strings like this

By discovering a field in the returned two-dimensional array that has a [mid], it returns a result of 3520617028999724. Exactly the corresponding.

Therefore: through the API interface to publish microblogging, receive its success after the return of the array, extract its mid to convert mid to 62 into the string, and then publish the UID combination of Weibo.

C # source code

<summary>///from a Sina Weibo address to obtain this microblog ID///</summary>///<param name= "url" > A Weibo address </param>///<returns> Weibo id</returns> public static string Getidfromurl (String u
    
            RL) {String mid = Getmidfromurl (URL); if (string. IsNullOrEmpty (mid)) {return string.
            Empty;
            else {return mid2id (mid); }///<summary>///from a Sina Weibo address to get this Weibo mid///</summary>/// <param name= "url" > a micro-blog address </param>///<returns> Weibo mid</returns> public static St Ring Getmidfromurl (string url) {if (string).
            IsNullOrEmpty (URL)) {return ""; } if (URL. IndexOf ('? ')!=-1) {url = URL. Substring (0, URL.
          IndexOf ('? '));  Regex reg = new Regex (@ ^http://e\.)?
            weibo\.com/[0-9a-za-z]+/(? <id>[0-9a-za-z]+) $ ", regexoptions.ignorecase); Match match = Reg.
            Match (URL); if (match. Success) {return match.
            Result ("${id}");
        Return ""; ///<summary>///converts Sina microblogging mid to ID///</summary>///<param name= "mid" 
        > Weibo mid</param>///<returns> return microblogging id</returns> public static string Mid2id (string mid)
            {String id = ' "; for (int i = Mid. Length-4; i >-4;
                i = i-4)//From the last 4 bytes to read the URL character {int offset1 = i < 0? 0:i; int len = i < 0? Mid.
                Length% 4:4; var str = Mid.
    
                Substring (Offset1, Len);
                str = str62toint (str); if (Offset1 > 0)//If it is not the first group, it is less than 7 bit 0 {while (str). LEngth < 7) {str = "0" + str;
            } id = str + ID;
        } return ID; ///<summary>///Sina Weibo id converted to mid///</summary>///<param name= "id" &G
        t; Weibo id</param>///<returns> return to Weibo mid</returns> public static string Id2mid (String id)
            {String mid = "", strtemp;
    
            int Startidex, Len; for (var i = ID. Length-7; i >-7;
                i = i-7)//From the last 7 bytes read Mid {Startidex = i < 0? 0:i; Len = i < 0? Id.
                Length% 7:7; strtemp = ID.
                Substring (Startidex, Len);
            Mid = IntToStr62 (Convert.ToInt32 (strtemp)) + mid;
        return mid; //62 into 10 binary public static string Str62toint (String str62) {Int64 i64 = 0; for (int i = 0; i < str62. Length; i++) {Int64 Vi = (Int64) Math.pow (str62.
                Length-i-1));
                Char t = str62[i];
            i64 + + Vi * GetInt10 (t.tostring ()); Return i64.
        ToString ();
            //10 into a 62-in-system public static string IntToStr62 (int int10) {string s62 = "";
            int r = 0;
                while (int10!= 0) {r = int10% 62;
                S62 = Get62key (r) + s62;
            int10 = INT10/62;
        return S62; }//62 dictionary private static string Str62keys = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVT
        XYZ "; Gets the 62-binary integer for the key private static Int64 GetInt10 (string key) {return Str62keys.
        IndexOf (key); //Gets the key private static string Get62key (int int10) {if (int10) corresponding to the 62 binary integer < 0 | |
            int10 >) return ""; Return Str62keys.
        Substring (int10, 1); }

The API provided by Sina is entered directly in the browser

Http://api.t.sina.com.cn/queryid.json?mid=z8ElgBLeQ&isBase62=1&type=1
returns
{"id": " 3520617028999724 "}
    
http://api.t.sina.com.cn/querymid.json?id=3520617028999724
return
{" Mid ":" Z8elgbleq "}

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/csharp/

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.