Httpclient 請求客戶端 服務端代碼

客戶端

public static String parseObject(final Object object) {
        ObjectMapper om=new ObjectMapper();
        String ret=null;
        try {
            ret=om.writeValueAsString(object);
        } catch (Exception e) {
            // TODO Auto-generated catch block
        }
        return ret;
    }

@RequestMapping("/staffListJson")
    @ResponseBody
    public String staffListJson(HttpServletRequest request){


        Map<String, String> paramsMap = new HashMap<String, String>();
        paramsMap.put("key1","value1");
        paramsMap.put("key2","value2");
        paramsMap.put("key3","value3");
        HttpClient httpclient = new HttpClient();//https://api.mch.weixin.qq.com/pay/unifiedorder
        String url = "http://127.0.0.1:8088/H-web/product/test1.htm";
        PostMethod post = new PostMethod(url);
        String info = null;
         try {  
             RequestEntity entity = new StringRequestEntity(parseObject(paramsMap), "text/xml","UTF-8");  
             post.setRequestEntity(entity);  
             httpclient.executeMethod(post);
             int code = post.getStatusCode();  
             if (code == HttpStatus.SC_OK){
                byte[] responseBody = post.getResponseBody();
                info = new String(responseBody, "UTF-8");
                JSONArray jsoin = JSONArray.parseArray(info);
                List<Object> list = (List<Object>) JSONArray.toJSON(jsoin);
                System.out.println("=========="+list+"++++++++++");

             }
         } catch (Exception ex) {  
             ex.printStackTrace();  
         } finally {  
             post.releaseConnection();  
         }


        return dp.toJson(null);
    } 

服務端

/**
     * 
     * @return
     */
    @RequestMapping("/test1")
    public void test1(HttpServletRequest req,HttpServletResponse resp)throws Exception{


          req.setCharacterEncoding("UTF-8");  
//        InputStream is = req.getInputStream();  
          System.out.println("contenttype"+req.getContentType());  
          resp.setContentType("application/xml;charset=utf-8");  
          String isStr = "";  
//        StringBuilder sb = new StringBuilder();;  
          try {  
           List<ProductPO>list = productService.getProductList("0",0);
//         isStr = convertStreamToString(is);  
           isStr = JSONArray.toJSONString(list);
//         sb.append(isStr);  
//         sb.append("i am back from server!");  
          } catch (Exception e1) {  
           e1.printStackTrace();  
          }  
          OutputStream out = resp.getOutputStream();  
          OutputStreamWriter ouputw = new OutputStreamWriter(out,"UTF-8");   
//        String str2 = sb.toString();  
          System.out.println("***最後返回******:"+isStr+"***************");  
          ouputw.write(isStr);  
          ouputw.flush();  
          ouputw.close();  
    }


    public static String parseObject(final Object object) {
        ObjectMapper om=new ObjectMapper();
        String ret=null;
        try {
            ret=om.writeValueAsString(object);
        } catch (Exception e) {
            // TODO Auto-generated catch block
        }
        return ret;
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章