List of usage examples for org.apache.commons.httpclient NameValuePair NameValuePair
public NameValuePair()
From source file:mesquite.tol.lib.BaseHttpRequestMaker.java
public static boolean postToServer(String s, String URI, StringBuffer response) { HttpClient client = new HttpClient(); PostMethod method = new PostMethod(URI); method.addParameter("OS", StringEscapeUtils .escapeHtml(System.getProperty("os.name") + "\t" + System.getProperty("os.version"))); method.addParameter("JVM", StringEscapeUtils .escapeHtml(System.getProperty("java.version") + "\t" + System.getProperty("java.vendor"))); NameValuePair post = new NameValuePair(); post.setName("post"); post.setValue(StringEscapeUtils.escapeHtml(s)); method.addParameter(post);/*from w w w . ja va 2s .c om*/ method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); return executeMethod(client, method, response); }
From source file:com.griddynamics.jagger.coordinator.http.client.ExchangeClient.java
private String exchangeData(String url, Serializable obj) throws IOException { PostMethod method = new PostMethod(urlBase + url); NameValuePair pair = new NameValuePair(); pair.setName(MESSAGE);//w w w. ja v a2s . co m pair.setValue(SerializationUtils.toString(obj)); method.setQueryString(new NameValuePair[] { pair }); try { int returnCode = httpClient.executeMethod(method); log.debug("Exchange response code {}", returnCode); return method.getResponseBodyAsString(); } finally { try { method.releaseConnection(); } catch (Throwable e) { log.error("Cannot release connection", e); } } }
From source file:com.funambol.json.dao.JsonDAOImpl.java
public JsonResponse addItem(String token, String jsonObject, long since) throws HttpException, IOException { String request = Utility.getUrl(jsonServerUrl, resourceType, ADD_ITEM_URL); if (log.isTraceEnabled()) { log.trace("JsonDAOImpl: start addItem; since=" + since); }//from w w w . ja v a 2s .c o m PostMethod post = new PostMethod(request); post.setRequestHeader(Utility.TOKEN_HEADER_NAME, token); post.setRequestEntity(new StringRequestEntity(jsonObject)); if (since != 0) { NameValuePair nvp_since = new NameValuePair(); nvp_since.setName("since"); nvp_since.setValue("" + since); NameValuePair[] nvp = { nvp_since }; post.setQueryString(nvp); } if (log.isTraceEnabled()) { log.trace("addItem Request: " + request); } if (Configuration.getConfiguration().isDebugMode()) { if (log.isTraceEnabled()) { log.trace("JSON to add " + jsonObject); } } int statusCode = 0; String responseBody = null; try { statusCode = httpClient.executeMethod(post); responseBody = post.getResponseBodyAsString(); } finally { post.releaseConnection(); } if (log.isTraceEnabled()) { log.trace("JsonDAOImpl: statusCode " + statusCode + "; added item" + responseBody); log.trace("JsonDAOImpl: item added"); } JsonResponse response = new JsonResponse(statusCode, responseBody); return response; }
From source file:com.funambol.json.dao.JsonDAOImpl.java
public JsonResponse updateItem(String token, String id, String jsonObject, long since) throws HttpException, IOException { String request = Utility.getUrl(jsonServerUrl, resourceType, UPDATE_ITEM_URL) + Utility.URL_SEP + id; if (log.isTraceEnabled()) { log.trace(/*from w w w . ja va2s . co m*/ "JsonDAOImpl: start updateItem with id:" + id + " and since=" + since + " sessionid:" + token); } PutMethod put = new PutMethod(request); put.setRequestHeader(Utility.TOKEN_HEADER_NAME, token); put.setRequestEntity(new StringRequestEntity(jsonObject)); if (since != 0) { NameValuePair nvp_since = new NameValuePair(); nvp_since.setName("since"); nvp_since.setValue("" + since); NameValuePair[] nvp = { nvp_since }; put.setQueryString(nvp); } if (log.isTraceEnabled()) { log.trace("updateItem Request: " + request); } if (Configuration.getConfiguration().isDebugMode()) { if (log.isTraceEnabled()) { log.trace("JSON to update " + jsonObject); } } int statusCode = 0; String responseBody = null; try { statusCode = httpClient.executeMethod(put); responseBody = put.getResponseBodyAsString(); } finally { put.releaseConnection(); } if (log.isTraceEnabled()) { log.trace("JsonDAOImpl: item with id:" + id + " updated; response " + responseBody); } JsonResponse response = new JsonResponse(statusCode, responseBody); return response; }
From source file:com.funambol.json.dao.JsonDAOImpl.java
public JsonResponse removeItem(String token, String id, long since) throws HttpException, IOException { String request = Utility.getUrl(jsonServerUrl, resourceType, REMOVE_ITEM_URL) + Utility.URL_SEP + id; if (log.isTraceEnabled()) { log.trace("JsonDAOImpl: start removeItem with id:" + id + "and since=" + since); }/*from w w w . j av a2s . com*/ DeleteMethod remove = new DeleteMethod(request); if (since != 0) { NameValuePair nvp_since = new NameValuePair(); nvp_since.setName("since"); nvp_since.setValue("" + since); NameValuePair[] nvp = { nvp_since }; remove.setQueryString(nvp); } if (log.isTraceEnabled()) { log.trace("JsonDAOImpl: removeItem request:" + request); } remove.setRequestHeader(Utility.TOKEN_HEADER_NAME, token); int statusCode = 0; String responseBody = null; try { statusCode = httpClient.executeMethod(remove); responseBody = remove.getResponseBodyAsString(); } finally { remove.releaseConnection(); } if (log.isTraceEnabled()) { log.trace("JsonDAOImpl: deleteItem response " + responseBody); log.trace("JsonDAOImpl: item with id:" + id + " removed"); } JsonResponse response = new JsonResponse(statusCode, responseBody); return response; }
From source file:com.funambol.json.dao.JsonDAOImpl.java
public JsonResponse removeAllItems(String token, long since) throws HttpException, IOException { String request = Utility.getUrl(jsonServerUrl, resourceType, REMOVE_ITEM_URL); if (log.isTraceEnabled()) { log.trace("JsonDAOImpl: start removeAllItems"); }/*from w ww . java 2 s . co m*/ DeleteMethod remove = new DeleteMethod(request); if (since != 0) { NameValuePair nvp_since = new NameValuePair(); nvp_since.setName("since"); nvp_since.setValue("" + since); NameValuePair[] nvp = { nvp_since }; remove.setQueryString(nvp); } if (log.isTraceEnabled()) { log.trace("JsonDAOImpl: removeAllItem request:" + request); } remove.setRequestHeader(Utility.TOKEN_HEADER_NAME, token); int statusCode = 0; String responseBody = null; try { statusCode = httpClient.executeMethod(remove); responseBody = remove.getResponseBodyAsString(); } finally { remove.releaseConnection(); } if (log.isTraceEnabled()) { log.trace("JsonDAOImpl: deleteAllItem response " + responseBody); } JsonResponse response = new JsonResponse(statusCode, responseBody); return response; }
From source file:com.funambol.json.api.dao.FunambolJSONApiDAO.java
/** * *//*w w w. java 2s . c om*/ protected String sendGetRequest(String REQUEST, long since, long until) throws IOOperationException { String response = null; GetMethod method = null; try { if (log.isTraceEnabled()) { log.trace("\nREQUEST:" + REQUEST); } method = new GetMethod(REQUEST); HttpClient httpClient = new HttpClient(); if (since != 0 && until != 0) { NameValuePair nvp_since = new NameValuePair(); nvp_since.setName("since"); nvp_since.setValue("" + since); NameValuePair nvp_until = new NameValuePair(); nvp_until.setName("until"); nvp_until.setValue("" + until); NameValuePair[] nvp = { nvp_since, nvp_until }; method.setQueryString(nvp); } if (this.sessionid != null) { method.setRequestHeader("Authorization", this.sessionid); } printMethodParams(method); printHeaderFields(method.getRequestHeaders(), "REQUEST"); int code = httpClient.executeMethod(method); response = method.getResponseBodyAsString(); if (log.isTraceEnabled()) { log.trace("RESPONSE code: " + code); } printHeaderFields(method.getResponseHeaders(), "RESPONSE"); } catch (Exception e) { throw new IOOperationException("Error GET Request ", e); } finally { if (method != null) { method.releaseConnection(); } } return response; }