百度 API 生成短网址自己测试的例子
来源:互联网 发布:铁路工程概预算软件 编辑:程序博客网 时间:2024/03/29 19:29
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;
}
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
0 0
- 百度 API 生成短网址自己测试的例子
- Java调用百度API生成短网址
- 调用百度api 生成短网址
- 使用短网址服务--百度的短网址API
- 百度,信鸽推送,百度短网址API
- 调用百度短网址API接口进行短网址的转换(Java程序的实现)
- java调用百度短网址api
- java调用百度短网址api
- 百度短网址API java HttpClient
- Google短网址的API
- 简单的生成短网址
- 调用百度API去生成短链接
- 百度短网址
- 百度短网址接口
- 百度短网址接口
- 如何创建自己的短网址服务
- 短网址API设计
- 使用PHP生成短网址的方案
- 2 Add Two Numbers
- Chp15 网络编程
- 猴子吃桃问题
- HDU 2200 Eddy's AC难题(组合数学)
- 基于FS4412嵌入式系统移植(8) linux内核调试之printk
- 百度 API 生成短网址自己测试的例子
- css word-wrap和word-break强制换行
- 第12周项目1实现复数类的运算符重载3
- Spring-Spring事务管理
- 细数5款主流NoSQL数据库到底哪家强?
- PDF阅读器---UIDocumentInteractionController
- 跳马问题
- 第十一周上机实践项目2(1):存储班长信息的学生类
- 第十三周上机实践(1).虚函数