`

短网址 生成

 
阅读更多

package com.top.nhm.manage.action.channel;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;

public class XXX {

    public static void getUrl() {

        char[] shortdwz = new char[100];
        char[] line2 = null;
        String line;
        char[] suitable = null;

        try {
            String strurl = "type=t.cn&url=http://bj.xf.kuai365.com/newf/33533.html";
            URL url = new URL("http://tinyurl.duapp.com/create.php");   // 伟大的度娘,无需用户登陆授权就可直接调用API
            // URL url = new URL("http://126.am/api!shorten.action");
            // 网易的API必须另外要用户的key做参数,其他门户没有找到公开的API
            URLConnection connection = url.openConnection();  // 打开url连接
            connection.setDoOutput(true); // 设置为输出
            PrintWriter out = new PrintWriter(connection.getOutputStream());
            out.write(strurl); // 写入短网址到PORT请求
            out.flush();
            out.close();
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            while ((line = in.readLine()) != null)  // 得到返回的信息
            {
                line2 = line.toCharArray();
            }
            for (int i = 0; i < line2.length; i++)
                if (line2[i] == 'h') {
                    int k = i, j;
                    for (j = 0; line2[k] != '"'; j++) {
                        if (line2[k] != '\\')
                            shortdwz[j] = line2[k];
                        else
                            j--;
                        k++;
                    }
                    suitable = new char[j];
                    for (int temp = 0; temp < j; temp++)
                        suitable[temp] = shortdwz[temp];
                    break;
                }
        } catch (Exception e) {
            System.out.println("发送POST请求出现异常!" + e);
            e.printStackTrace();
        }
        // if (shortdwz[7] != 'd' || shortdwz[8] != 'w' || shortdwz[9] != 'z') {
        // JOptionPane.showMessageDialog(null, "原网址输入错误或无效!", "错误", JOptionPane.ERROR_MESSAGE);
        // }
        line = new String(suitable);
        System.out.println(line);
    }

    public static void main(String[] args) {
        getUrl();
    }
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics