• 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;
        }
  • 相关阅读:
    成功后的迷失
    让主参与的人生
    生命的见证
    谁偷走了我的安全感
    简单理解分页原理
    WebApp开发入门
    程序员必需知道的搜索引擎的搜索技巧
    提高网页打开速度技巧
    十分钟了解HTTP协议
    简单实现页面临摹设计图功能
  • 原文地址:https://www.cnblogs.com/yshyee/p/7461969.html
Copyright © 2020-2023  润新知