安卓基础-get post请求与提交数据

发布者:世界美景
发布于:2017-11-03 19:16

为什么写这篇文章呢?  很简单的东西 但我发现随着时间变化 

越基础的东西忘得越多 准备写博客回忆一下 而且目前在做逆向的东西 会写一些小型APP 但网络请求这块根本没必要用框架 所以算是写个工具博客专门给自己看的 到时候直接粘代码 当然如果能帮到一些朋友 那更好


GET方式提交数据

new Thread(){
    public void run() {
        try {
            //GET请求方式的特点:在url后面组拼数据 根据给的数据不同 接口会返回相应的数据
            String path = "http://192.168.1.103:8080/web/Test?imei="+IMEI+"&=model"+MODEL);
            URL url = new URL(path);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(5000);
            int code = conn.getResponseCode();
            if(code == 200){
                InputStream is = conn.getInputStream();
                String result = StreamTools.readStream(is);
                Message msg = Message.obtain();
                msg.what = SUCCESS;
                msg.obj = result;
                handler.sendMessage(msg);
            }else{
                Message msg = Message.obtain();
                msg.what = ERROR;
                handler.sendMessage(msg);
            }
        } catch (Exception e) {
            e.printStackTrace();
            Message msg = Message.obtain();
            msg.what = ERROR;
            handler.sendMessage(msg);
        }
    };
}.start();

POST方式提交数据

new Thread(){
    public void run() {
        //路径不需要组拼
        String path = "http://192.168.1.103:8080/web/Test";
        try {
            URL url = new URL(path);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            //1.设置请求方式为POST
            conn.setRequestMethod("POST"); //注意单词必须大写.
            conn.setConnectTimeout(5000);
            //2.设置http请求数据的类型为表单类型
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            //3.设置给服务器写的数据的长度
            // imei=865161564315&model=huawei G720-t01
            String data = "imei="+IMEI+"&model="+MODEL;
            conn.setRequestProperty("Content-Length", String.valueOf(data.length()));
            //4.记得指定要给服务器写数据
            conn.setDoOutput(true);
            //5.开始向服务器写数据
            conn.getOutputStream().write(data.getBytes());
            int code = conn.getResponseCode();
            if(code == 200){
                InputStream is = conn.getInputStream();
                String result = StreamTools.readStream(is);
                Message msg = Message.obtain();
                msg.what = SUCCESS;
                msg.obj = result;
                handler.sendMessage(msg);
            }else{
                Message msg = Message.obtain();
                msg.what = ERROR;
                handler.sendMessage(msg);
            }
        } catch (Exception e) {
            e.printStackTrace();
            Message msg = Message.obtain();
            msg.what = ERROR;
            handler.sendMessage(msg);
        }
    };
}.start();
  protected static final int SUCCESS = 1;
    protected static final int ERROR = 2;
    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {

            switch (msg.what) {
                case ERROR:
                    Toast.makeText(MainActivity.this, "网络异常", Toast.LENGTH_SHORT)
                            .show();
                    break;

                case SUCCESS:
                    String http = (String) msg.obj;
                    break;
            }
        }
    };







GET请求和POST请求的区别

优缺点
* GET请求
优点:使用非常方便,只需要在url后面组拼数据。
缺点:数据在url的后面组拼,不安全。有数据长度限制。
* POST请求
优点:安全,数据不是在url后面组拼而是通过流的方式写给服务器。数据长度不受限制
缺点:编写麻烦。

数据提交
* GET请求
1. 需要在url后面组拼提交的数据
* POST请求
1. 不需要组拼任何的数据
2. 必须指定请求的数据类型,是一个经过url编码的表单数据。Content-Type
3. 以流的方式把数据写给服务器,所以必须指定提交数据的长度。Content-Length

总结

就安全来说 其实这两种根本就没区别 如果是https的话会好一些 但如果用charles 或者Fiddler 抓包大部分数据都可以看到

https的话顶多不好做一些劫持 但如果找到应用内部请求代码 可以用replace把 https 在内部转成http 在重打包就可以了 

至于说实际过程中用到那种 根据当前情况来 有时候后台会限制你用get还是post 大部分还是想用那种用那种

GB2312和GBK

GB2312
1980年发布,标准共收录6763个汉字,其中一级汉字3755个,二级汉字3008个;同时,GB2312收录了包括拉丁字母、希腊字母、日文平假名及片假名字母、俄语西里尔字母在内的682个全角字符。
GB 2312的出现,基本满足了汉字的计算机处理需要,它所收录的汉字已经覆盖中国大陆99.75%的使用频率。
对于人名、古汉语等方面出现的罕用字,GB2312不能处理,这导致了后来GBK及GB18030汉字字符集的出现。
GBK
1995年发布,是在GB2312-80标准基础上的内码扩展规范,共收录了21003个汉字,完全兼容GB2312-80标准。

string.getBytes(String charsetName)和new String(byte[] bytes, String charsetName)

  1. byte[] bytes = string.getBytes(String charsetName)
    将字符串按指定的编码转化为byte数组,默认采用本地码表
  2. new String(byte[] bytes, String charsetName)
    将byte数组按指定的编码转化为字符串

注意:出现乱码时不要修改文件,修改后无论怎么切换编码都是错的了

提交数据中含有中文的注意事项

客户端

//提交的数据中含有中文时,将字符串IMEI按照编码UTF-8进行编码
URLEncoder.encode(IMEI, "UTF-8");

HttpClient的GET请求

GET请求

new Thread(){
    public void run() {
        try {
            String path = "接口地址";
            //1.打开浏览器
            HttpClient client = new DefaultHttpClient();
            //2.输入地址或者数据
            HttpGet httpGet = new HttpGet(path);
            //3.敲回车
            HttpResponse response = client.execute(httpGet);
            //获取状态码
            int code = response.getStatusLine().getStatusCode();
            if(code == 200){
                InputStream is = response.getEntity().getContent();
                String result = StreamTools.readStream(is);
                Message msg = Message.obtain();
                msg.what = SUCCESS;
                msg.obj = result;
                handler.sendMessage(msg);
            }else{
                Message msg = Message.obtain();
                msg.what = ERROR;
                handler.sendMessage(msg);
            }
        } catch (Exception e) {
            e.printStackTrace();
            Message msg = Message.obtain();
            msg.what = ERROR;
            handler.sendMessage(msg);
        }
    };
}.start();

总结

以下会出现的问题 在androidStudio中httpClient会报错找不到 HttpClient都被google废弃了 在6.0 SDK中直接拿掉了
需要要在build.gradle 进行引用 就可以正常使用了 

andoird{
useLibrary 'org.apache.http.legacy'
}  





声明:该文观点仅代表作者本人,转载请注明来自看雪