java调用百度短网址api

来源:互联网 发布:最真实的网络射击游戏 编辑:程序博客网 时间:2024/03/28 21:21
  1. 转载地址:http://blog.csdn.net/ljasdf123/article/details/21828535
    1. 生成短网址

    请求:向dwz.cn/create.php发送post请求,发送数据包括url=长网址

    返回:json格式的数据

    1. status!=0 出错,查看err_msg获得错误信息(UTF-8编码)
    2. 成功,返回生成的短网址 tinyurl字段
    1. 自定义短网址

    请求:向dwz.cn/create.php发送post请求,发送数据包括url=长网址&alias=自定义网址

    返回:json格式的数据

    1. Status!=0 出错,查看err_msg获得错误信息(UTF-8编码)
    2. 成功,返回生成的短网址 tinyurl字段
    1. 显示原网址

    请求:向dwz.cn/query.php发送post请求,发送数据包括tinyurl=查询的短地址

    返回:json格式的数据

    1. status!=0 出错,查看err_msg获得错误信息(UTF-8编码)
    2. 成功,返回原网址 longurl字段

    示例程序:

    生成短网址
    <?php
    $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)
    {
    /**错误处理*/
    echo iconv('UTF-8','GBK',$arrResponse['err_msg'])."\n";
    }
    /** tinyurl */
    echo $arrResponse['tinyurl']."\n";
    ?>其他接口使用如上

示例:java调用百度短网址api

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. import java.util.ArrayList;  
  2. import java.util.List;  
  3.   
  4. import org.apache.http.HttpResponse;  
  5. import org.apache.http.NameValuePair;  
  6. import org.apache.http.client.entity.UrlEncodedFormEntity;  
  7. import org.apache.http.client.methods.HttpPost;  
  8. import org.apache.http.impl.client.DefaultHttpClient;  
  9. import org.apache.http.message.BasicNameValuePair;  
  10. import org.apache.http.util.EntityUtils;  
  11.   
  12. import com.alibaba.fastjson.JSON;  
  13. import com.alibaba.fastjson.JSONObject;  
  14.   
  15. /** 
  16.  * 生成短网址并返回 
  17.  * @author: Jerri  
  18.  * @date: 2014年3月22日下午9:58:54 
  19.  */  
  20. public class GenerateShortUrlUtil {  
  21.     public static DefaultHttpClient httpclient;  
  22.     static {  
  23.         httpclient = new DefaultHttpClient();  
  24.         httpclient = (DefaultHttpClient) HttpClientConnectionManager  
  25.                 .getSSLInstance(httpclient); // 接受任何证书的浏览器客户端  
  26.     }  
  27.       
  28.     /** 
  29.      * 生成端连接信息 
  30.      *  
  31.      * @author: Jerri  
  32.      * @date: 2014年3月22日下午5:31:15 
  33.      */  
  34.     public static String  generateShortUrl(String url) {  
  35.         try {  
  36.             HttpPost httpost = new HttpPost("http://dwz.cn/create.php");  
  37.             List<NameValuePair> params = new ArrayList<NameValuePair>();  
  38.             params.add(new BasicNameValuePair("url", url)); // 用户名称  
  39.             httpost.setEntity(new UrlEncodedFormEntity(params,  "utf-8"));  
  40.             HttpResponse response = httpclient.execute(httpost);  
  41.             String jsonStr = EntityUtils  
  42.                     .toString(response.getEntity(), "utf-8");  
  43.             System.out.println(jsonStr);  
  44.             JSONObject object = JSON.parseObject(jsonStr);  
  45.             System.out.println(object.getString("tinyurl"));  
  46.             return object.getString("tinyurl");  
  47.         } catch (Exception e) {  
  48.             e.printStackTrace();  
  49.             return "Error";  
  50.         }  
  51.           
  52.     }  
  53.       
  54.     /** 
  55.      * 测试生成端连接 
  56.      * @param args 
  57.      * @author: Jerri  
  58.      * @date: 2014年3月22日下午5:34:05 
  59.      */  
  60.     public static void main(String []args){  
  61.         generateShortUrl("http://help.baidu.com/index");  
  62.     }  
  63. }  
0 0
原创粉丝点击