Java调用百度API生成短网址

由于百度短网址旧API更新,故更新下原博客内容(仅供参考),希望小伙伴们以官方API为主,以防影响使用。
官方API:https://dwz.cn/console/apidoc在这里插入图片描述

package com.hive.utils.string;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.io.BufferedReader;
import java.io.InputStreamReader;

import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;

public class BaiduDwz {
	final static String CREATE_API = "https://dwz.cn/admin/v2/create";
	final static String TOKEN = "你的token"; // TODO:设置Token

	class UrlResponse {
		@SerializedName("Code")
		private int code;

		@SerializedName("ErrMsg")
		private String errMsg;

		@SerializedName("LongUrl")
		private String longUrl;

		@SerializedName("ShortUrl")
		private String shortUrl;

		public int getCode() {
			return code;
		}

		public void setCode(int code) {
			this.code = code;
		}

		public String getErrMsg() {
			return errMsg;
		}

		public void setErrMsg(String errMsg) {
			this.errMsg = errMsg;
		}

		public String getLongUrl() {
			return longUrl;
		}

		public void setLongUrl(String longUrl) {
			this.longUrl = longUrl;
		}

		public String getShortUrl() {
			return shortUrl;
		}

		public void setShortUrl(String shortUrl) {
			this.shortUrl = shortUrl;
		}
	}

	/**
	 * 创建短网址
	 *
	 * @param longUrl
	 *            长网址:即原网址
	 * @return 成功:短网址 失败:返回空字符串
	 */
	public static String createShortUrl(String longUrl) {
		String params = "{\"url\":\"" + longUrl + "\"}";

		BufferedReader reader = null;
		try {
			// 创建连接
			URL url = new URL(CREATE_API);
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection.setDoOutput(true);
			connection.setDoInput(true);
			connection.setUseCaches(false);
			connection.setInstanceFollowRedirects(true);
			connection.setRequestMethod("POST"); // 设置请求方式
			connection.setRequestProperty("Content-Type", "application/json"); // 设置发送数据的格式
			connection.setRequestProperty("Token", TOKEN); // 设置发送数据的格式");

			// 发起请求
			connection.connect();
			OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8"); // utf-8编码
			out.append(params);
			out.flush();
			out.close();

			// 读取响应
			reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
			String line;
			String res = "";
			while ((line = reader.readLine()) != null) {
				res += line;
			}
			reader.close();

			// 抽取生成短网址
			UrlResponse urlResponse = new Gson().fromJson(res, UrlResponse.class);
			if (urlResponse.getCode() == 0) {
				return urlResponse.getShortUrl();
			} else {
				System.out.println(urlResponse.getErrMsg());
			}

			return ""; // TODO:自定义错误信息
		} catch (IOException e) {
			// TODO
			e.printStackTrace();
		}
		return ""; // TODO:自定义错误信息
	}

	public static void main(String[] args) {
		String res = createShortUrl("https://blog.csdn.net/wh_forever/article/details/49247991");
		System.out.println(res);

	}

}

以下就是生成的短网址链接,通过Java调用百度API

https://dwz.cn/qf2k9MMv

PS:调用接口前先填写token,token获取方式参考百度官方API https://dwz.cn/console/apidoc

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zsw_sh

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值