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

https://img.mukewang.com/5cbd211d0001bc6a04020077.jpg

慕盖茨4494581
浏览 2217回答 7
7回答

慕斯709654

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

呼啦一阵风

那就用 get 吧!

收到一只叮咚

可以指定请求方式: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;    }

暮色呼如

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

繁星coding

是的,请参考HttpServlet类中的doGet,doPost,Serivce三个方法,springMVC的封装底层依然不能离开这三个方法。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java