美文网首页Java
urlconnection post请求

urlconnection post请求

作者: 挑战者666888 | 来源:发表于2018-10-23 15:02 被阅读0次
    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();
    
        }
    
    }
    
    

    相关文章

      网友评论

        本文标题:urlconnection post请求

        本文链接:https://www.haomeiwen.com/subject/wukazftx.html