微信摇一摇周边--获取access_token

1.首先创建一个工具类

public class ConTools {

    private HttpURLConnection connection;

    /**
     * TangRen 2016-4-9 下午3:11:28
     */

    public HttpURLConnection ConTools(String urlString) {
        try {
            URL url = new URL(urlString);
            connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setRequestMethod("POST");
            connection.setConnectTimeout(5000);
            connection.setReadTimeout(5000);
            connection.setInstanceFollowRedirects(true);
            connection.setRequestProperty("Content-Type",
                    "application/json; encoding=UTF-8");
            connection.connect();

        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }

        return connection;

    }
}

Read工具类

public class ReadTools {
    /**
     * TangRen 2016-4-9 下午3:22:23
     */
    private BufferedReader reader;
    private StringBuffer sb;

    public StringBuffer ReadTools(HttpURLConnection connection) {
        try {
            reader = new BufferedReader(new InputStreamReader(
                    connection.getInputStream()));
            String line;
            sb = new StringBuffer("");
            while ((line = reader.readLine()) != null) {
                line = new String(line.getBytes(), "UTF-8");
                sb.append(line);
            }
            reader.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return sb;
    }
}

接下来就是申请TOKEN

public class ApplyToken {

    /**
     * @param args
     * @author TangRen
     * @date 2016-4-14 上午9:41:19
     */
    private static ReadTools read;
    private static HttpURLConnection connection;
    static StringBuffer readTools = null;

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ApplyToken();
    }

    public static String  ApplyToken() {
        // TODO Auto-generated constructor stub
        try {
            URL url = new URL(AppURL.url_applytoken);
            connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setRequestMethod("GET");
            connection.setConnectTimeout(5000);
            connection.setReadTimeout(5000);
            connection.setInstanceFollowRedirects(true);
            connection.setRequestProperty("Content-Type",
                    "application/json; encoding=UTF-8");
            connection.connect();

            DataOutputStream outputStream=new DataOutputStream(connection.getOutputStream());

            BufferedWriter writer=new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));

            writer.flush();
            writer.close();

            read = new ReadTools();
            readTools = read.ReadTools(connection);
            System.out.println(readTools);
            // 断开连接
            connection.disconnect();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        return readTools.toString();

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

吴唐人

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

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

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

打赏作者

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

抵扣说明:

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

余额充值