百度 API 生成短网址自己测试的例子
2016-05-26 09:00
447 查看
package com.zhidao.www;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class TestShort {
public static void main(String[] args) {
// TODO Auto-generated method stub
String httpUrl = "http://apis.baidu.com/3023/shorturl/shorten";
String httpArg = "url_long=http%3A%2F%2Fapistore.baidu.com%2Fastore%2Fshopready%2F1973.html";
String jsonResult = request(httpUrl, httpArg);
System.out.println(jsonResult);
}
/**
* @param urlAll
* :请求接口
* @param httpArg
* :参数
* @return 返回结果
*/
public static String request(String httpUrl, String httpArg) {
BufferedReader reader = null;
String result = null;
StringBuffer sbf = new StringBuffer();
httpUrl = httpUrl + "?" + httpArg;
try {
URL url = new URL(httpUrl);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setRequestMethod("GET");
// 填入apikey到HTTP header
connection.setRequestProperty("apikey", "0bcae0071cafdafadd3a85acd22493af");
connection.connect();
InputStream is = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String strRead = null;
while ((strRead = reader.readLine()) != null) {
sbf.append(strRead);
sbf.append("\r\n");
}
reader.close();
result = sbf.toString();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
最后附上api的地址 http://apistore.baidu.com/apiworks/servicedetail/1466.html
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class TestShort {
public static void main(String[] args) {
// TODO Auto-generated method stub
String httpUrl = "http://apis.baidu.com/3023/shorturl/shorten";
String httpArg = "url_long=http%3A%2F%2Fapistore.baidu.com%2Fastore%2Fshopready%2F1973.html";
String jsonResult = request(httpUrl, httpArg);
System.out.println(jsonResult);
}
/**
* @param urlAll
* :请求接口
* @param httpArg
* :参数
* @return 返回结果
*/
public static String request(String httpUrl, String httpArg) {
BufferedReader reader = null;
String result = null;
StringBuffer sbf = new StringBuffer();
httpUrl = httpUrl + "?" + httpArg;
try {
URL url = new URL(httpUrl);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setRequestMethod("GET");
// 填入apikey到HTTP header
connection.setRequestProperty("apikey", "0bcae0071cafdafadd3a85acd22493af");
connection.connect();
InputStream is = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String strRead = null;
while ((strRead = reader.readLine()) != null) {
sbf.append(strRead);
sbf.append("\r\n");
}
reader.close();
result = sbf.toString();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
最后附上api的地址 http://apistore.baidu.com/apiworks/servicedetail/1466.html
相关文章推荐
- 第十三周项目—阅读程序,并运行结果(纯虚函数)
- 深入理解JavaScript系列+ 深入理解javascript之执行上下文
- 第十一周阅读项目(5)
- 猴子吃桃问题
- Random Forest 随机森林
- 基于FS4412嵌入式系统移植(8) linux内核调试之printk
- Java中判断对象是否相等的equals()方法使用教程
- css word-wrap和word-break强制换行
- 我们应该如何去了解JavaScript引擎的工作原理 系列
- HDU4355(三分)
- Chp15 网络编程
- NoSQL数据库概览及其与SQL语法的比较
- 小代码 代码整理
- ssh框架中的basedao
- 2 Add Two Numbers
- Codeforces Round #353 (Div. 2) B.Restoring Painting
- ARC_xp_20160526
- CodeForces 592B
- 求素数问题
- UVa1368 DNA Consensus String