为了账号安全,请及时绑定邮箱和手机立即绑定

java后台的接口,没有指明用post还是get,就意味着都可以吗?

java后台的接口,没有指明用post还是get,就意味着都可以吗?

慕盖茨4494581 2019-03-29 23:19:00
查看完整描述

7 回答

?
慕斯709654

TA贡献1840条经验 获得超5个赞

是滴,不写的话,默认支持所有HTTP请求方法,见如下参考文档

https://img1.sycdn.imooc.com//5cbd212e0001676208000391.jpg

查看完整回答
反对 回复 2019-04-22
?
呼啦一阵风

TA贡献1802条经验 获得超6个赞

那就用 get 吧!


查看完整回答
反对 回复 2019-04-22
?
收到一只叮咚

TA贡献1821条经验 获得超4个赞

可以指定请求方式:connection.setRequestMethod("POST");如下代码

public static String httpPostWithJson(String ecUrl, String params) {

        BufferedReader reader = null;

        HttpURLConnection connection = null;

        try {

            URL url = new URL(ecUrl);

            connection = (HttpURLConnection) url.openConnection();

            // 创建连接

            connection.setDoOutput(true);

            connection.setDoInput(true);

            connection.setRequestMethod("POST");

            connection.setUseCaches(false);

            connection.setInstanceFollowRedirects(true);

            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

            connection.connect();


            // POST请求

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

            out.writeBytes(params);

            out.flush();

            out.close();


            // 读取响应

            reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));

            String lines;

            StringBuffer sb = new StringBuffer("");

            while ((lines = reader.readLine()) != null) {

                lines = new String(lines.getBytes(), "utf-8");

                sb.append(lines);

            }

            return sb.toString();

        } catch (MalformedURLException e) {

            logger.error("httpPostWithJsonMalformedURLException error", e);

            e.printStackTrace();

        } catch (UnsupportedEncodingException e) {

            logger.error("httpPostWithJsonUnsupportedEncodingException error", e);

            e.printStackTrace();

        } catch (IOException e) {

            logger.error("httpPostWithJsonIOException error", e);

            e.printStackTrace();

        } finally {

            try {

                if (null != reader) {

                    reader.close();

                }

                if (null != connection) {

                    connection.disconnect();

                }

            } catch (IOException e) {

                e.printStackTrace();

            }

        }

        return null;

    }


查看完整回答
反对 回复 2019-04-22
?
暮色呼如

TA贡献1853条经验 获得超9个赞

是的,需要限制的话用RequestMethod指定下


查看完整回答
反对 回复 2019-04-22
?
繁星coding

TA贡献1797条经验 获得超4个赞

是的,请参考HttpServlet类中的doGet,doPost,Serivce三个方法,springMVC的封装底层依然不能离开这三个方法。


查看完整回答
反对 回复 2019-04-22
  • 7 回答
  • 0 关注
  • 2216 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号