java代碼 發送GET、POST請求

來源:互聯網
上載者:User

     /**
     * 發送GET請求
     *
     * @param url url地址
     * @return JSONObject
     */

    public static String getAjaxDataFromURLForSMS(String url) {
        long begin = System.currentTimeMillis();
        LOG.info("request:" + url);
        String result = null;
        BufferedReader rd = null;

        try {
            URL u = new URL(url);
            InputStream is = u.openStream();
            rd = new BufferedReader(new InputStreamReader(is, "utf-8"));
            StringBuffer sb = new StringBuffer();

            String line;
            while ((line = rd.readLine()) != null) {
                sb.append(line);
            }

            long end = System.currentTimeMillis();
            LOG.info("response[" + (end - begin) + "]:" + sb.toString());
            result = sb.toString();

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (rd != null) {
                try {
                    rd.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

        return result;
    }
   

   /**

     * @return XMLData
     */
    public static Document getXMLDataFromURL(String url) {
        long begin = System.currentTimeMillis();
        LOG.info("request:" + url);
        SAXBuilder saxBuilder = new SAXBuilder();
        Document doc = null;
        try {
            URL u = new URL(url);
            InputStream in = u.openStream();

            doc = saxBuilder.build(in);

        } catch (Exception e) {
            e.printStackTrace();
        }
        if (LOG.isInfoEnabled()) {
            long end = System.currentTimeMillis();
            XMLOutputter outp = new XMLOutputter();
            LOG.info("response[" + (end - begin) + "]:" + outp.outputString(doc));
        }

        return doc;
    }

   /**

     * 發送GET請求
     *
     * @param url
     *            url地址
     * @return JSONObject
     */

    public static JSONObject getJSONDataFromURL(String url) {
        long begin = System.currentTimeMillis();
        LOG.info("request:" + url);
        JSONObject jsonObj = null;
        BufferedReader rd = null;

        try {
            URL u = new URL(url);
            InputStream is = u.openStream();
            rd = new BufferedReader(new InputStreamReader(is, "utf-8"));
            StringBuffer sb = new StringBuffer();

            String line;
            while ((line = rd.readLine()) != null) {
                sb.append(line);
            }

            long end = System.currentTimeMillis();
            LOG.info("response[" + (end - begin) + "]:" + sb.toString());
            jsonObj = new JSONObject(sb.toString());

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        } finally {
            if (rd != null) {
                try {
                    rd.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

        return jsonObj;
    }

    /**
     * 發送POST請求
     *
     * @param url
     *            url地址 params 參數
     * @return JSONObject
     */

    public static JSONObject getJSONDataFromURL(String url, String params) {
        long begin = System.currentTimeMillis();
        LOG.info("request:" + url);
        JSONObject jsonObj = null;

        HttpURLConnection huc = null;
        BufferedReader in = null;
        InputStreamReader isr = null;
        InputStream is = null;

        StringBuffer sb = new StringBuffer(333);

        try {

            URL u = new URL(url);
            huc = (HttpURLConnection) u.openConnection();
            huc.setRequestMethod("POST");
            huc.setDoOutput(true);
            huc.getOutputStream().write(params.getBytes("utf-8"));
            huc.getOutputStream().flush();
            huc.getOutputStream().close();
            is = huc.getInputStream();
            isr = new InputStreamReader(is, "utf-8");
            in = new BufferedReader(isr);
            String line = null;

            while ((line = in.readLine()) != null) {
                sb.append(line);
            }

            long end = System.currentTimeMillis();
            LOG.info("response[" + (end - begin) + "]:" + sb.toString());
            jsonObj = new JSONObject(sb.toString());

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        } finally {
            try {
                huc.disconnect();
                is.close();
                isr.close();
                in.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return jsonObj;
    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.