@RequestMapping(value = "/", method = RequestMethod.GET) String get() { return "Hello from get"; } @RequestMapping(value = "/post", method = RequestMethod.POST) String post(HttpServletRequest req, HttpServletResponse resp) { Map<String, String[]> m = req.getParameterMap(); return "Hello from post"; } public static void main(String[] args) throws Exception { CloseableHttpClient client = HttpClients.createDefault(); HttpPost httpPost = new HttpPost("http://localhost:8080/testweb/post"); httpPost.setHeader("Content-type", "application/x-www-form-urlencoded"); Map<String,String> map = new HashMap<>(); map.put("yonghu","tony"); map.put("mima","25"); List<NameValuePair> nvps = new ArrayList<NameValuePair>(); if(map != null){ for (Map.Entry<String, String> entry : map.entrySet()) { nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } } httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8")); CloseableHttpResponse response = client.execute(httpPost); HttpEntity entity = response.getEntity(); String respContent = EntityUtils.toString(entity,"UTF-8"); }
HttpClient Post Example
最新推荐文章于 2024-08-06 17:13:24 发布