博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
通过Android重审GET和POST请求
阅读量:6838 次
发布时间:2019-06-26

本文共 3495 字,大约阅读时间需要 11 分钟。

hot3.png

学习Java web的时候总是背GET和POST的区别,根本不知道GET和POST有什么区别!

百度一下它们的区别就有答案!但是不能理解!

POST的代码:

public void run() {                String path = "http://10.31.2.6:8080/06_Server/servlet/login";                try {                    URL url = new URL(path);                                        HttpURLConnection conn = (HttpURLConnection) url.openConnection();                                        conn.setRequestMethod("POST");                    conn.setReadTimeout(5000);                    conn.setConnectTimeout(5000);                                        /**                     * 设置POST请求特殊的东西                     * name="+ URLEncoder.encode(name) +"&pass=" + pass                     */                    //拼接出要提交的数据的字符串                    String data = "name=" + URLEncoder.encode(name) + "&pass=" + pass;                    //添加post请求的两行属性                    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");                    conn.setRequestProperty("Content-Length", data.length() + "");                                        //将协议体提交给服务器                    //打开输出流                    conn.setDoOutput(true);                    //拿到输出流                    OutputStream os = conn.getOutputStream();                    //放到输出流中,提交到服务器                    os.write(data.getBytes());                                        conn.connect();                    if(conn.getResponseCode() == 200) {                        InputStream in = conn.getInputStream();                        //将输入流变成字符串                        String result = Utils.fromStream2String(in);                                                    Message msg = handler.obtainMessage();                        msg.obj = result;                        handler.sendMessage(msg);                                                                    } else {                        System.out.println("系统出错");                    }                                    } catch (Exception e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }                            }

GET的代码:

public void run() {                String path = "http://10.31.2.6:8080/06_Server/servlet/login?name="+ URLEncoder.encode(name) +"&pass=" + pass;                try {                    URL url = new URL(path);                                        HttpURLConnection conn = (HttpURLConnection) url.openConnection();                                        conn.setRequestMethod("GET");                    conn.setReadTimeout(5000);                    conn.setConnectTimeout(5000);                                        conn.connect();                                        if(conn.getResponseCode() == 200) {                        InputStream in = conn.getInputStream();                        //将输入流变成字符串                        String result = Utils.fromStream2String(in);                                                    Message msg = handler.obtainMessage();                        msg.obj = result;                        handler.sendMessage(msg);                                                                    } else {                        System.out.println("系统出错");                    }                                    } catch (Exception e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }                            }

代码中就体现了GET和POST请求的区别!

转载于:https://my.oschina.net/Sheamus/blog/491970

你可能感兴趣的文章
How to use Ivy to manage your OSGi bundle dependen
查看>>
51CTO六周年贺礼:译文一篇
查看>>
在CentOS 7上安装Kafka
查看>>
JSP笔记——1.运行原理
查看>>
Nginx 实现AJAX跨域请求
查看>>
Centos7 搭建InfluxDB+Grafana监控平台
查看>>
Atom 安装插件失败问题 (安装源被墙) | 解决办法
查看>>
storm实战入门:开发简易Topology实例
查看>>
我的友情链接
查看>>
mysql读写分离实战
查看>>
使用Kubespray部署生产可用的Kubernetes集群(1.11.2)
查看>>
MPICH2(r8733)的错误容忍
查看>>
我的友情链接
查看>>
Python:contextlib模块——上下文管理器工具
查看>>
数制之十进制转成其他进制
查看>>
运用bind()和connect()函数
查看>>
帧、数据报、段、Frame Datagram Segment Packet Fragment
查看>>
推荐一款免费的AD审计工具------Netwrix AD变更通知工具
查看>>
Xcode8控制台输出大量无用信息的解决方案
查看>>
【简单的留言本】用HTML新增的数据库实现
查看>>