java地址短链接_长连接地址转为短链接

import java.util.LinkedList;

import java.util.List;

import org.apache.http.NameValuePair;

import org.apache.http.client.entity.UrlEncodedFormEntity;

import org.apache.http.client.methods.CloseableHttpResponse;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.http.impl.client.HttpClients;

import org.apache.http.message.BasicNameValuePair;

import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSON;

import com.alibaba.fastjson.JSONArray;

import com.alibaba.fastjson.JSONObject;

/***

* 短链接转换工具类

*

* @author Administrator

*

*/

public class ShortUrlHelper {

public static CloseableHttpClient httpClient;

static {

httpClient = HttpClients.createDefault();

}

/**

* 将长链接转为短链接(调用的新浪的短网址API)

*

* @param url

* 需要转换的长链接url

* @return 返回转换后的短链接

*/

public static String convertSinaShortUrl(String url) {

try {

// 调用新浪API

HttpPost post = new HttpPost("http://api.t.sina.com.cn/short_url/shorten.json");

List params = new LinkedList();

// 必要的url长链接参数

params.add(new BasicNameValuePair("url_long", url));

// 必要的新浪key

params.add(new BasicNameValuePair("source", "3271760578"));

post.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));

CloseableHttpResponse response = httpClient.execute(post);

// 得到调用新浪API后成功返回的json字符串

// url_short : 短链接地址 type:类型 url_long:原始长链接地址

String json = EntityUtils.toString(response.getEntity(), "utf-8");

JSONArray jsonArray = JSONArray.parseArray(json);

JSONObject object = (JSONObject) jsonArray.get(0);

return object.getString("url_short");

} catch (Exception e) {

e.printStackTrace();

return "";

}

}

/**

* 将长链接转为短链接(调用的百度短网址API)

*

* @param url

* 需要转换的长链接url

* @return 返回转换后的短链接

*/

public static String convertBaiDuShortUrl(String url) {

try {

// 调用百度API

HttpPost post = new HttpPost("http://www.dwz.cn/create.php");

List params = new LinkedList();

// 必要的url长链接参数

params.add(new BasicNameValuePair("url", url));

post.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));

CloseableHttpResponse response = httpClient.execute(post);

// 得到调用百度API后成功返回的json字符串

// tinyurl : 短链接地址 status:0 表示转换成功 非0表示转换失败 longurl:原始长链接地址 err_msg:错误信息

String jsonStr = EntityUtils.toString(response.getEntity(), "utf-8");

JSONObject object = JSON.parseObject(jsonStr);

return object.getString("tinyurl");

} catch (Exception e) {

e.printStackTrace();

return "";

}

}

/**

* 测试

* @param args

*/

public static void main(String []args){

String tinyurl = convertBaiDuShortUrl("http://news.sina.com.cn/gov/xlxw/2018-09-05/doc-ihiixyeu3395739.shtml");

System.out.println(tinyurl);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中,可以使用HttpURLConnection类来发送HTTP请求并获取HTTP响应。要实现链接重定向到链接,可以按照以下步骤进行操作: 1. 使用HttpURLConnection类发送到链接的HTTP请求。 2. 从HTTP响应中获取重定向的URL。 3. 使用重定向的URL发送HTTP请求。 4. 从HTTP响应中获取所需的数据。 以下是一个简单的示例代码,演示如何实现链接重定向到链接: ``` import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class RedirectUrlExample { public static void main(String[] args) throws Exception { String shortUrl = "http://t.cn/xxxxxxx"; // 链接 String longUrl = getLongUrl(shortUrl); // 获取链接 System.out.println(longUrl); // 使用链接发送HTTP请求 URL url = new URL(longUrl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); String inputLine; StringBuilder response = new StringBuilder(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); // 处理HTTP响应 System.out.println(response.toString()); } private static String getLongUrl(String shortUrl) throws Exception { URL url = new URL(shortUrl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("HEAD"); int status = conn.getResponseCode(); if (status == HttpURLConnection.HTTP_MOVED_TEMP || status == HttpURLConnection.HTTP_MOVED_PERM || status == HttpURLConnection.HTTP_SEE_OTHER) { String location = conn.getHeaderField("Location"); if (location != null) { return location; } } return shortUrl; } } ``` 在上面的示例中,getLongUrl()方法发送到链接的HTTP请求,并从HTTP响应中获取重定向到的URL。然后,使用重定向的URL发送HTTP请求,获取所需的数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值