当前位置:编程学习 > JAVA >>

java代码 发送GET、POST请求

 /**
     * 发送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();
  &

补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,