博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
dwz.cn接口java客户端实现
阅读量:2432 次
发布时间:2019-05-10

本文共 1884 字,大约阅读时间需要 6 分钟。

项目中需要用到短网址,使用了

java客户端工具类实现如下:

需要的jar包,Maven配置

com.alibaba
fastjson
1.2.6
org.apache.httpcomponents
httpclient
4.3.6
源码

package com.kuanrf.common.util;import java.util.ArrayList;import java.util.List;import org.apache.http.HttpResponse;import org.apache.http.NameValuePair;import org.apache.http.client.config.CookieSpecs;import org.apache.http.client.config.RequestConfig;import org.apache.http.client.entity.UrlEncodedFormEntity;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.JSONObject;/** * 短链接生成 *  * @author lujun.chen * @version [版本号, 2015年10月10日] * @see [相关类/方法] * @since [产品/模块版本] */public class GenerateShortUrlUtil {	public static CloseableHttpClient httpclient;	static {		RequestConfig config = RequestConfig.custom()				.setCookieSpec(CookieSpecs.IGNORE_COOKIES).build();		httpclient = HttpClients.custom().setDefaultRequestConfig(config)				.build();	}	/**	 * 生成端连接信息	 * 	 * @author: Jerri	 * @date: 2014年3月22日下午5:31:15	 */	public static String generateShortUrl(String url) {		try {			HttpPost httpost = new HttpPost("http://www.dwz.cn/create.php");			List
params = new ArrayList
(); params.add(new BasicNameValuePair("url", url)); httpost.setEntity(new UrlEncodedFormEntity(params, "utf-8")); HttpResponse response = httpclient.execute(httpost); String jsonStr = EntityUtils .toString(response.getEntity(), "utf-8"); JSONObject object = JSON.parseObject(jsonStr); return object.getString("tinyurl"); } catch (Exception e) { e.printStackTrace(); return "Error"; } }}

转载地址:http://ukvmb.baihongyu.com/

你可能感兴趣的文章
主从复制
查看>>
冒泡排序
查看>>
直接插入排序
查看>>
选择排序
查看>>
归并排序
查看>>
快速排序
查看>>
堆排序
查看>>
mongodb命令大全
查看>>
Mongodb分片实践
查看>>
Mongodb主从架构
查看>>
Mongodb副本集
查看>>
Mongodb高可用架构
查看>>
保护眼睛
查看>>
idea的一些常用设置设置
查看>>
jvm远程调试
查看>>
新机器配置
查看>>
好的编程原则
查看>>
服务器部署
查看>>
Apache日常配置
查看>>
MapReduce
查看>>