Java

urlconnection post请求

2018-10-23  本文已影响0人  挑战者666888
package com.rayootech.activiti.util;

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.net.HttpURLConnection;

import java.net.URL;

public class Test {

    public static void main(String[] args) {

        String loadJSON = loadJSON("http://www.szse.cn/api/report/ShowReport/data?SHOWTYPE=JSON&CATALOGID=1826&TABKEY=tab1&txtDate=2018-10-19&txtEndDate=2018-10-19&random=0.07181609944620826");

        System.out.println(loadJSON);

    }

    private static String loadJSON(String url) {

        StringBuilder stringBuilder = new StringBuilder();

        BufferedReader in = null;

        HttpURLConnection urlConnection = null;

        try {

            URL postUrl = new URL(url);

            urlConnection = (HttpURLConnection) postUrl.openConnection();

            urlConnection.setDoInput(true);

            urlConnection.setDoOutput(true);

            urlConnection.setUseCaches(false);

            urlConnection.setRequestMethod("GET");

            if (urlConnection.getResponseCode() == 200) {

                in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "utf-8"));//防止乱码

                String inputLine = null;

                while ((inputLine = in.readLine()) != null) {

                    stringBuilder.append(inputLine);

                }

            }

        } catch (Exception e) {

            log.error("获取抽签参数访问链接有误urlConnection异常:{}",e);

            //            log.info

            //("获取抽签参数访问链接有误");

            e.printStackTrace();

        } finally {

            try {

                in.close();

                urlConnection.disconnect();

            } catch (Exception e) {

                log.error("关闭流异常:{}",e);

            }

        }

        return stringBuilder.toString();

    }

}

上一篇下一篇

猜你喜欢

热点阅读