private String executePostMethod(final HttpClient client, final PostMethod method) { int response = 0; String result = null; try { final Profiler profiler = new Profiler(); profiler.start(); response = client.executeMethod(method); if (response == HttpStatus.SC_OK) { result = method.getResponseBodyAsString(); profiler.stop(); if (s_logger.isDebugEnabled()) { s_logger.debug("POST " + _serviceUrl + " response :" + result + ", responding time: " + profiler.getDurationInMillis() + " ms"); } } else { profiler.stop(); s_logger.error("Invalid response code : " + response + ", from : " + _serviceUrl + ", method : " + method.getParameter("method") + " responding time: " + profiler.getDurationInMillis()); } } catch (final HttpException e) { s_logger.error("HttpException from : " + _serviceUrl + ", method : " + method.getParameter("method")); } catch (final IOException e) { s_logger.error("IOException from : " + _serviceUrl + ", method : " + method.getParameter("method")); } catch (final Throwable e) { s_logger.error("Exception from : " + _serviceUrl + ", method : " + method.getParameter("method") + ", exception :", e); } finally { method.releaseConnection(); } return result; }
public String getCharacterEncoding() { return postMethod.getParameter("http.protocol.content-charset").getValue(); }
public String getHTTPMethod() { return postMethod.getParameter("http.protocol.version").getValue(); }
private String executePostMethod(final HttpClient client, final PostMethod method) { int response = 0; String result = null; try { final Profiler profiler = new Profiler(); profiler.start(); response = client.executeMethod(method); if (response == HttpStatus.SC_OK) { result = method.getResponseBodyAsString(); profiler.stop(); if (s_logger.isDebugEnabled()) { s_logger.debug("POST " + _serviceUrl + " response :" + result + ", responding time: " + profiler.getDurationInMillis() + " ms"); } } else { profiler.stop(); s_logger.error("Invalid response code : " + response + ", from : " + _serviceUrl + ", method : " + method.getParameter("method") + " responding time: " + profiler.getDurationInMillis()); } } catch (final HttpException e) { s_logger.error("HttpException from : " + _serviceUrl + ", method : " + method.getParameter("method")); } catch (final IOException e) { s_logger.error("IOException from : " + _serviceUrl + ", method : " + method.getParameter("method")); } finally { method.releaseConnection(); } return result; } }
@Test public void addHttpPostParams() { //given ErrorReportSender sender = new ErrorReportSender(); Map<String, String> values = new HashMap<>(); values.put("key1", "value1"); values.put("key2", "value2"); PostMethod method = new PostMethod(); //when sender.addHttpPostParams(values, method); //then assertEquals("value1", method.getParameter("key1").getValue()); assertEquals("value2", method.getParameter("key2").getValue()); }
@Test public void addHttpPostParamsIsNullSafe() { //given ErrorReportSender sender = new ErrorReportSender(); Map<String, String> values = new HashMap<>(); values.put("key2", null); PostMethod method = new PostMethod(); //when sender.addHttpPostParams(values, method); //then assertEquals("null", method.getParameter("key2").getValue()); } }