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

      

     

    1、StringEntity

     

    try{  

        HttpPost httpPost = new HttpPost(url);  

        //param参数,可以为param="key1=value1&key2=value2"的一串字符串,或者是jsonObject 

         String param1="key1=value1&key2=value2"

    JSONObject param2= new JSONObject();  

        param2.put("key1", "value1");  

        param2.put("key2t"," value2");  

        StringEntity stringEntity = new StringEntity(param1);  

        StringEntity stringEntity = new StringEntity(param2.toString());  

        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){  

    }  

    2、UrlEncodedFormEntity

    List<NameValuePair> pairs = new ArrayList<NameValuePair>();  

    NameValuePair pair1 = new BasicNameValuePair("supervisor", supervisorEt.getEditableText().toString());  

    NameValuePair pair2 = new BasicNameValuePair("content", superviseContentEt.getEditableText().toString());  

    NameValuePair pair3 = new BasicNameValuePair("userId", String.valueOf(signedUser.getId()));  

    pairs.add(pair1);  

    pairs.add(pair2);  

    pairs.add(pair3);  

    httpPost.setEntity(new UrlEncodedFormEntity(pairs, HTTP.UTF_8)) 

  • 相关阅读:
    微软职位内部推荐-SENIOR SDE
    微软职位内部推荐-Senior Network Engineer
    微软职位内部推荐-Principal Dev Manager
    微软职位内部推荐-SDE II
    微软职位内部推荐-Sr DEV
    【转载】NIO服务端序列图
    【转载】NIO客户端序列图
    同步与异步
    Linux查找命令
    Spring中Bean的实例化
  • 原文地址:https://www.cnblogs.com/keyi/p/8512496.html
Copyright © 2011-2022 走看看