zoukankan      html  css  js  c++  java
  • HttpClient Restful Post 请求

        public static void main(String[] args) {
            SbVo sb = new SbVo();
            sb.setBusiness("SB");
            sb.setIphone("123456789");
            
            String param = new Gson().toJson(sb);
            String url = "http://127.0.0.1:9001/ssfwpt/sb/test";
            
            System.out.println(httpPost(url, param));
        }
    
        public static String httpPost(final String url, final String param) {
            String result = null;
            
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost postRequest = new HttpPost(url);
    
            postRequest.addHeader("Content-type", "application/json");
    
            try {
                StringEntity input = new StringEntity(param);
    
                input.setContentType("application/json");
                
                postRequest.setEntity(input);
    
                HttpResponse response = httpClient.execute(postRequest);
                
                if (response.getStatusLine().getStatusCode() == 200) {
                    HttpEntity entity = response.getEntity();
                    if (null != entity) {
                        result = EntityUtils.toString(entity, "UTF-8");
                    }
                }
            } catch (UnsupportedEncodingException ex) {
                Logger.getLogger(Httpclienttest.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(Httpclienttest.class.getName()).log(Level.SEVERE, null, ex);
            } finally{
                httpClient.getConnectionManager().shutdown();
            }
    
            return result;
        }
  • 相关阅读:
    react的路由以及传值方法
    三连击
    给网页添加鼠标样式
    单词统计(续)
    个人课程总结
    构建之法阅读笔记02
    构建之法阅读笔记01
    第十六周总结
    计算最长英语单词链
    第十五周总结
  • 原文地址:https://www.cnblogs.com/yshyee/p/7461969.html
Copyright © 2011-2022 走看看