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;
    }
  • 相关阅读:
    config Doku wiki
    [转载]【python】ipython与python的区别
    数组和指针
    C++初始化数据成员
    RSA算法原理
    C++四种cast
    百度笔试准备2
    百度笔试准备1
    百度面试准备
    C++的空类中默认产生哪些类成员函数
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/4546277.html
Copyright © 2011-2022 走看看