Java calls Baidu short URL api

Source: Internet
Author: User


  1. How to call Baidu short url api?
    1. Generate short URL

      Request: Send a postrequest to dwz.cn/create.php and a data packet URL

      Return data in json format

      1. Status! = 0 error, view err_msg get error message (UTF-8 code)
      2. Success. The generated short URL tinyurl field is returned.
        1. Custom short URL

          Request: Send a postrequest to dwz.cn/create.php, and send a data package, URL, and URL

          Return data in json format

          1. Status! = 0 error, view err_msg get error message (UTF-8 code)
          2. Success. The generated short URL tinyurl field is returned.
            1. Show Original website

              Request: Send a postrequest to dwz.cn/query.php and send a data packet to the short address of tinyurl.pdf

              Return data in json format

              1. Status! = 0 error, view err_msg get error message (UTF-8 code)
              2. Succeeded. The original URL longurl field is returned.

                Example program:

                Generate short URL
                $ Ch = curl_init ();
                Curl_setopt ($ ch, CURLOPT_URL, "http://dwz.cn/create.php ");
                Curl_setopt ($ ch, CURLOPT_POST, true );
                Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true );
                $ Data = array ('url' => 'HTTP: // www.baidu.com /');
                Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ data );
                $ StrRes = curl_exec ($ ch );
                Curl_close ($ ch );
                $ ArrResponse = json_decode ($ strRes, true );
                If ($ arrResponse ['status'] = 0)
                {
                /** Handle errors */
                Echo iconv ('utf-8', 'gbk', $ arrResponse ['err _ msg ']). "\ n ";
                }
                /** Tinyurl */
                Echo $ arrResponse ['tinyurl']. "\ n ";
                ?> Other interfaces are as follows:

                Example: java calls Baidu short URL api
                Import java. util. arrayList; import java. util. list; import org. apache. http. httpResponse; import org. apache. http. nameValuePair; import org. apache. http. client. entity. urlEncodedFormEntity; import org. apache. http. client. methods. httpPost; import org. apache. http. impl. client. defaultHttpClient; import org. apache. http. message. basicNameValuePair; import org. apache. http. util. entityUtils; import com. alibaba. fastjson. JSON; import com. alibaba. fastjson. JSONObject;/*** generate a short URL and return * @ author: Jerri * @ date: 9:58:54, January 1, March 22, 2014 */public class GenerateShortUrlUtil {public static DefaultHttpClient httpclient; static {httpclient = new DefaultHttpClient (); httpclient = (DefaultHttpClient) HttpClientConnectionManager. getSSLInstance (httpclient); // browser client that accepts any certificate}/*** generate client connection information ** @ author: Jerri * @ date: march 22, 2014 5:31:15 */public static String generateShortUrl (String url) {try {HttpPost httpost = new HttpPost ("http://dwz.cn/create.php"); List
                               
                                
                Params = new ArrayList
                                
                                 
                (); Params. add (new BasicNameValuePair ("url", url); // user name httpost. setEntity (new UrlEncodedFormEntity (params, "UTF-8"); HttpResponse response = httpclient.exe cute (httpost); String jsonStr = EntityUtils. toString (response. getEntity (), "UTF-8"); System. out. println (jsonStr); JSONObject object = JSON. parseObject (jsonStr); System. out. println (object. getString ("tinyurl"); return object. getString ("tinyurl");} catch (Exception e) {e. printStackTrace (); return "Error" ;}}/*** test the producer connection * @ param args * @ author: Jerri * @ date: march 22, 2014 5:34:05 */public static void main (String [] args) {generateShortUrl ("http://help.baidu.com/index ");}}
                                
                               


Related Article

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.