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

  • 相关阅读:
    数据结构10——最短路径
    获取JVM转储文件的Java工具类
    如何测试这个方法--性能篇
    如何测试这个方法--功能篇
    使用WireMock进行更好的集成测试
    性能测试框架第二版
    如何使用Selenium来计算自动化测试的投资回报率?
    模糊断言
    如何从测试自动化中实现价值
    如何获取JVM堆转储文件
  • 原文地址:https://www.cnblogs.com/keyi/p/8512496.html
Copyright © 2011-2022 走看看