zoukankan      html  css  js  c++  java
  • http post 方法传递参数的2种方式

     try{
            HttpPost httpPost = new HttpPost(url);
            StringEntity stringEntity = new StringEntity(param);//param参数,可以为"key1=value1&key2=value2"的一串字符串
            stringEntity.setContentType("application/x-www-form-urlencoded");
            httpPost.setEntity(stringEntity);
            HttpClient client = new DefaultHttpClient(); 
            HttpResponse httpResponse = client.execute(httpPost);
            String result = EntityUtils.toString(httpResponse.getEntity(), HTTP.UTF_8);
        } catch(IOException e){
        }

    有的时候我们不想要通过下面的方式来传递参数,因为考虑请求接口时我比较喜欢的方式是直接把key和value连成一串,如"key1=value1&key2=value2"来作为参数,这样http get和post的方法都可以用同样的结构来作为参数,于是http post的方法请求服务器数据时可以用上面的方法来实现.

    List<NameValuePair>list = new ArrayList<NameValuePair>();
            for (int i = 0; i < keys.length; i++) {
                list.add(new BasicNameValuePair(keys[i], values[i]));
            }
            HttpPost httpRequst = new HttpPost(urlString);
            httpRequst.setEntity(new UrlEncodedFormEntity(list,HTTP.UTF_8));

    httpRequst.setEntity()这个方法是最主要的post传递的参数实现的方式了(不知道这样说对不对)

     httpEntity有AbstractHttpEntity, BasticHttpEntity, BasicManageEntity, BufferedHttpEntity, ByteArrayEntity, EntityTemplate,  FileEntity, HttpEntityWrapper, InputStreamEntity, SerializableEntityStringEntityUrlEncodedFormEntity

    转载:https://blog.csdn.net/yuleran/article/details/12655493

  • 相关阅读:
    【c++】中文设置
    《谁动了我的奶酪》读后感
    KMP算法的C++实现
    我也说说中文分词(上:基于字符串匹配)
    删除字符串中的空格
    linux jdk bin安装
    笔试题汇总
    栈的压入、弹出序列
    顺序打印矩阵
    二叉树镜像
  • 原文地址:https://www.cnblogs.com/xianfengzhike/p/9656295.html
Copyright © 2011-2022 走看看