使用get和post方式提交数据

来源:互联网 发布:淘宝卖家怎么开小号 编辑:程序博客网 时间:2024/05/07 01:29

GET方式提交数据

Android中Http提交get请求,需要把要提交的数据拼接在url后面

服务器端:

public class LoginServlet extends HttpServlet {    public void doGet(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        //拿到用户名        String name =  request.getParameter("name");        String pass =  request.getParameter("pass");        System.out.println(name + ";" + pass);        OutputStream os = response.getOutputStream();        //验证是否正确,写死了        if("asd".equals(name) && "123".equals(pass)){            //服务器端与客户端编码一致,防止出现乱码            os.write("登录成功".getBytes("utf-8"));        }        else{            os.write("登录失败QAQ".getBytes("utf-8"));        }    }    public void doPost(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        doGet(request, response);    }}

客户端:

    Handler handler = new Handler(){        public void handleMessage(android.os.Message msg) {            Toast.makeText(MainActivity.this, (String)msg.obj, 0).show();        }    };    public void click(View v) {        EditText et_name = (EditText) findViewById(R.id.et_name);        EditText et_pass = (EditText) findViewById(R.id.et_pass);        final String name = et_name.getText().toString();        final String pass = et_pass.getText().toString();        Thread t = new Thread(){            //网址是拼在url后面,注意不要加空格,否则提交不过去            String path = "http://10.66.121.5/WebGet/servlet/LoginServlet?name=" + name + "&pass=" + pass;            @Override            public void run() {                try {                    URL url = new URL(path);                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();                    conn.setRequestMethod("GET");                    conn.setConnectTimeout(5000);                    conn.setReadTimeout(5000);                    if(conn.getResponseCode() == 200){                        InputStream is = conn.getInputStream();                        String text = Utils.getTextFrom(is);                        Message msg = handler.obtainMessage();                        msg.obj = text;                        handler.sendMessage(msg);                    }                } catch (Exception e) {                    e.printStackTrace();                }            }        };        t.start();    }
public class Utils {    //创建专门从流里读数据的方法    public static String getTextFrom(InputStream is){        byte[] b = new byte[1024];        int len = 0;        //创建字节数组输出流,读取输入流的文本数据时,同步把数据写入数组输出流        ByteArrayOutputStream bos = new ByteArrayOutputStream();        try {            while((len = is.read(b)) != -1){                bos.write(b, 0, len);            }            //把字节数组输出流里的数据转换成字节数组            String text = new String(bos.toByteArray());            return text;        } catch (IOException e) {            e.printStackTrace();        }        //try出问题了就什么都读不到        return null;    }}

如果想要使用中文,则需要在服务器端修改编码,因为在服务器拿到字节数组时是用iso8859-1的码表把字节数组变成字符串的,浏览器是用utf-8把字节数组变成字符串,因此需要把name用iso8859-1的编码重新变成字节数组,再用utf-8的码表重新变成字符串,这时name就可以是一个中文了

    public void doGet(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        //拿到用户名        String name =  request.getParameter("name");        String pass =  request.getParameter("pass");        name = new String(name.getBytes("iso8859-1"),"utf-8");        pass = new String(pass.getBytes("iso8859-1"),"utf-8");        System.out.println(name + ";" + pass);        OutputStream os = response.getOutputStream();        //验证是否正确,写死了        if("一一".equals(name) && "123".equals(pass)){            os.write("登录成功".getBytes("utf-8"));        }        else{            os.write("登录失败QAQ".getBytes("utf-8"));        }    }
            //提交的数据需要经过url编码,英文和数字编码后不变,中文改变            String path = "http://10.66.121.5/WebGet/servlet/LoginServlet?name=" + URLEncoder.encode(name) + "&pass=" + pass;

Post方式提交数据

Post方式数据不是拼接在url后面的,而是放在请求的流当中传过来的,通过流写给服务器,在响应头处多了两个属性的字段,Content-Type(内容的类型)和Centent-Length(提交数据的长度),使得服务器处理数据时更为方便

        Thread t = new Thread(){            //Post提交就只是servlet网址,不用拼接字符串            String path = "http://10.66.121.5/WebGet/servlet/LoginServlet";            @Override            public void run() {                try {                    URL url = new URL(path);                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();                    conn.setRequestMethod("POST");                    conn.setConnectTimeout(5000);                    conn.setReadTimeout(5000);                    //拼接出要提交的数据的字符串                    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());                    if(conn.getResponseCode() == 200){                        InputStream is = conn.getInputStream();                        String text = Utils.getTextFrom(is);                        Message msg = handler.obtainMessage();                        msg.obj = text;                        handler.sendMessage(msg);                    }                } catch (Exception e) {                    e.printStackTrace();                }            }        };        t.start();
0 0
原创粉丝点击