zoukankan      html  css  js  c++  java
  • HttpPost发送Json

    1. 1.public static JSONObject post(String url,JSONObject json){    
    2. 2.        HttpClient client = new DefaultHttpClient();    
    3. 3.        HttpPost post = new HttpPost(url);    
    4. 4.        JSONObject response = null;    
    5. 5.        try {    
    6. 6.            StringEntity s = new StringEntity(json.toString());    
    7. 7.            s.setContentEncoding("UTF-8");    
    8. 8.            s.setContentType("application/json");    
    9. 9.            post.setEntity(s);    
    10. 10.                
    11. 11.            HttpResponse res = client.execute(post);    
    12. 12.            if(res.getStatusLine().getStatusCode() == HttpStatus.OK.value()){    
    13. 13.                HttpEntity entity = res.getEntity();    
    14. 14.                String charset = EntityUtils.getContentCharSet(entity);    
    15. 15.                response = new JSONObject(new JSONTokener(new InputStreamReader(entity.getContent(),charset)));    
    16. 16.            }    
    17. 17.        } catch (Exception e) {    
    18. 18.            throw new RuntimeException(e);    
    19. 19.        }    
    20. 20.        return response;    
    21. 21.    }    
  • 相关阅读:
    大数据面经
    mysql复习(2)
    java容器
    内存管理
    垃圾收集
    输入/输出流
    排序算法的稳定性及其汇总
    java传值与传引用
    linux复习6
    linux复习5
  • 原文地址:https://www.cnblogs.com/zey23/p/3974577.html
Copyright © 2011-2022 走看看