• 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

  • 相关阅读:
    各个版本中Notification对象创建的方法
    数据结构一:线性表
    安装eclipse中文汉化包后无法打开eclipse【转】
    在MFC里面使用自定义的OpenGL类进行绘图(基于VS2010)
    2016-2-25 我的博客开通了
    从C#到Swift原来这么简单,So Easy!
    CocoaPods安装及使用(包含重装步骤)
    Xcode键盘快捷键
    参考资料收集
    重温算法和数据结构:二分查找
  • 原文地址:https://www.cnblogs.com/xianfengzhike/p/9656295.html
Copyright © 2020-2023  润新知