zoukankan      html  css  js  c++  java
  • android 通过post方式提交数据的最简便有效的方法

    public boolean post(String username, String password) throws Exception {
    username = URLEncoder.encode(username);// 中文数据需要经过URL编码
    password = URLEncoder.encode(password);
    String params = "username=" + username + "&password=" + password; 
    byte[] data = params.getBytes();
    
    URL url = new URL(address);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setConnectTimeout(3000);
    //这是请求方式为POST
    conn.setRequestMethod("POST");
    //设置post请求必要的请求头
    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");// 请求头, 必须设置
    conn.setRequestProperty("Content-Length", data.length + "");// 注意是字节长度, 不是字符长度
    
    conn.setDoOutput(true);// 准备写出
    conn.getOutputStream().write(data);// 写出数据
    
    return conn.getResponseCode() == 200;
    }
  • 相关阅读:

    删与改

    基本操作
    名词解释
    Python内置函数(11)——complex
    Python内置函数(10)——float
    Python内置函数(9)——int
    Python内置函数(8)——bool
    Python内置函数(7)——sum
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/4546277.html
Copyright © 2011-2022 走看看