@Test public void postFormData() throws UnsupportedEncodingException { UpdateUserRequest request = new UpdateUserRequest("/user/updateUser"); request.setMethod(MethodTypeEnum.POST); request.setHttpContentType(ContentType.APPLICATION_FORM_URLENCODED); request.setUserId(1L); request.setUsername("李太白"); String result = HttpUtil.post(request.composeUrl(baseUrl, request.getQueryParameters()), null, request.getHttpContentType()); AbstractResponse response = JacksonUtil.readValue(result, request.getResponseClass()); log.info(response.toString()); }
public static String post(String requestUrl, Object data, ContentType contentType) { if (StringUtils.isBlank(requestUrl)) { throw new RuntimeException("requestUrl cann't be null"); } if (contentType == null) { throw new RuntimeException("contentType cann't be null"); } HttpPost post = new HttpPost(requestUrl); if (data != null) { StringEntity entity = new StringEntity(data.toString(), contentType); post.setEntity(entity); } logger.info("HttpClient request uri -> " + requestUrl); logger.info("HttpClient request body -> " + data); return executeRequest(post, contentType.getCharset()); }
HttpUtil { public static String post(String requestUrl, Object data, ContentType contentType) { if (StringUtils.isBlank(requestUrl)) { throw new RuntimeException("requestUrl cann't be null"); } if (contentType == null) { throw new RuntimeException("contentType cann't be null"); } HttpPost post = new HttpPost(requestUrl); if (data != null) { StringEntity entity = new StringEntity(data.toString(), contentType); post.setEntity(entity); } logger.info("HttpClient request uri -> " + requestUrl); logger.info("HttpClient request body -> " + data); return executeRequest(post, contentType.getCharset()); } }
HttpUtil { public static String post(String requestUrl, Object data, ContentType contentType) { if (StringUtils.isBlank(requestUrl)) { throw new RuntimeException("requestUrl cann't be null"); } if (contentType == null) { throw new RuntimeException("contentType cann't be null"); } HttpPost post = new HttpPost(requestUrl); if (data != null) { StringEntity entity = new StringEntity(data.toString(), contentType); post.setEntity(entity); } logger.info("HttpClient request uri -> " + requestUrl); logger.info("HttpClient request body -> " + data); return executeRequest(post, contentType.getCharset()); }  }
HttpUtil { public static String post(String requestUrl, Object data, ContentType contentType) { if (StringUtils.isBlank(requestUrl)) { throw new RuntimeException("requestUrl cann't be null"); } if (contentType == null) { throw new RuntimeException("contentType cann't be null"); } HttpPost post = new HttpPost(requestUrl); if (data != null) { StringEntity entity = new StringEntity(data.toString(), contentType); post.setEntity(entity); } logger.info("HttpClient request uri -> " + requestUrl); logger.info("HttpClient request body -> " + data); return executeRequest(post, contentType.getCharset()); }  static String get(String requestUrl); static String post(String requestUrl, Object data, ContentType contentType); static String uploadFile(String requestUrl, Object data); }
HttpUtil { public static String post(String requestUrl, Object data, ContentType contentType) { if (StringUtils.isBlank(requestUrl)) { throw new RuntimeException("requestUrl cann't be null"); } if (contentType == null) { throw new RuntimeException("contentType cann't be null"); } HttpPost post = new HttpPost(requestUrl); if (data != null) { StringEntity entity = new StringEntity(data.toString(), contentType); post.setEntity(entity); } logger.info("HttpClient request uri -> " + requestUrl); logger.info("HttpClient request body -> " + data); return executeRequest(post, contentType.getCharset()); }  static String get(String requestUrl); static String post(String requestUrl, Object data, ContentType contentType); static String uploadFile(String requestUrl, Object data);  }