精易论坛

标题: 【java】 TX短链接生成 [打印本页]

作者: junWord    时间: 2020-5-4 12:57
标题: 【java】 TX短链接生成
  1. 1.直接导入第三方jar
  2. <dependency>
  3.         <groupId>commons-httpclient</groupId>
  4.         <artifactId>commons-httpclient</artifactId>
  5.         <version>3.1</version>
  6. </dependency>

  7. <dependency>
  8.         <groupId>org.json</groupId>
  9.         <artifactId>json</artifactId>
  10.         <version>20190722</version>
  11. </dependency>
  12. 2.工具类代码
  13. import org.apache.commons.httpclient.HttpClient;
  14. import org.apache.commons.httpclient.HttpException;
  15. import org.apache.commons.httpclient.methods.GetMethod;
  16. import org.apache.commons.httpclient.methods.PostMethod;
  17. import org.apache.commons.httpclient.params.HttpMethodParams;
  18. import org.json.JSONObject;

  19. import java.io.IOException;
  20. import java.util.HashMap;
  21. import java.util.Map;
  22. import java.util.regex.Matcher;
  23. import java.util.regex.Pattern;

  24. public class HttpClientHelper {
  25.     public static JSONObject ShortUrl(String UrlLink) throws HttpException, IOException {
  26.         String ApiUrl="https://service.weibo.com/share/share.php?url="+UrlLink+"&title=texs";
  27.         // 创建httpClient实例对象
  28.         HttpClient httpClient = new HttpClient();
  29.         // 设置httpClient连接主机服务器超时时间:15000毫秒
  30.         httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(15000);
  31.         // 创建post请求方法实例对象
  32.         PostMethod postMethod = new PostMethod(ApiUrl);
  33.         // 设置post请求超时时间
  34.         postMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 60000);
  35.         postMethod.addRequestHeader("Content-Type", "application/json");
  36.         httpClient.executeMethod(postMethod);
  37.         String result = postMethod.getResponseBodyAsString();
  38.         postMethod.releaseConnection();
  39.         String regex ="scope.short_url = "(.*?) "";
  40.         Pattern pattern = Pattern.compile(regex);
  41.         Matcher matcher = pattern.matcher(result);
  42.         String ApiText="";
  43.         while(matcher.find()){
  44.             ApiText = matcher.group(1);
  45.         }
  46.         Map<String, Object> map = new HashMap<String, Object>();
  47.         map.put("Code","200");
  48.         map.put("ShortUrl",ApiText.trim());
  49.         map.put("LongUrl",UrlLink);
  50.         JSONObject jObject = new JSONObject(map);
  51.         return jObject;
  52.     }

  53.     public static void main(String[] args) throws HttpException, IOException {
  54.         String url = "http://www.juni.ink";
  55.         System.out.println("short_url->"+ShortUrl(url));
  56.     }
  57. }
复制代码



作者: 动鸡不淳    时间: 2020-5-4 13:08
我是为了那个回贴奖励才回复的
作者: junWord    时间: 2020-5-4 13:10
skychat 发表于 2020-5-4 13:08
我是为了那个回贴奖励才回复的

(>^ω^<)感谢支持
作者: along6    时间: 2020-5-4 14:35

我是为了那个回贴奖励才回复的
作者: 菜就多多练    时间: 2020-5-4 16:58
易语言这么简单我都没搞明白,这个我就不看了
作者: 久居时光    时间: 2020-5-4 23:18
我是为了那个回贴奖励才回复的
作者: 蓝疯子    时间: 2020-5-5 22:58
httpclient读网页 正则取短连接 这个不是新浪的么..





欢迎光临 精易论坛 (https://125.confly.eu.org/) Powered by Discuz! X3.4