List of usage examples for org.apache.commons.httpclient.methods PostMethod PostMethod
public PostMethod(String paramString)
From source file:net.mojodna.searchable.solr.SolrIndexer.java
@Override protected void delete(final Serializable key) throws IndexingException { final Element delete = new Element("delete").addContent( new Element("query").addContent(IndexSupport.COMPOUND_ID_FIELD_NAME + ":" + key.toString())); // now do something with the delete block try {/* w ww. j a v a2s . c o m*/ final XMLOutputter out = new XMLOutputter(Format.getPrettyFormat()); final String deleteString = out.outputString(delete); final PostMethod post = new PostMethod(solrPath); post.setRequestEntity(new StringRequestEntity(deleteString, "text/xml", "UTF-8")); log.debug("Deleting:\n" + deleteString); getHttpClient().executeMethod(post); if (!isBatchMode()) { commit(); } } catch (final IOException e) { throw new IndexingException(e); } }
From source file:com.assemblade.client.AbstractClient.java
protected <T> T add(String path, T object, TypeReference<T> type) throws ClientException { PostMethod post = new PostMethod(baseUrl + path); try {/*from w w w . j a v a 2 s .c o m*/ try { post.setRequestEntity( new StringRequestEntity(mapper.writeValueAsString(object), "application/json", null)); } catch (IOException e) { throw new CallFailedException("Failed to serialize a request object", e); } int statusCode = executeMethod(post); if (statusCode == 200) { try { return mapper.readValue(post.getResponseBodyAsStream(), type); } catch (IOException e) { throw new CallFailedException("Failed to deserialize a response object", e); } } else { throw new InvalidStatusException(200, statusCode); } } finally { post.releaseConnection(); } }
From source file:de.avanux.livetracker.sender.LocationSender.java
private void sendPositionData(Configuration configuration, float lat, float lon) { String id = configuration.getID(); log.debug("Using URL " + configuration.getPositionReceiverUrl()); log.debug("Sending position data: id=" + id + " lat=" + lat + " lon=" + lon); PostMethod method = new PostMethod(configuration.getPositionReceiverUrl()); NameValuePair[] data = { new NameValuePair(LocationMessage.TRACKING_ID, id), new NameValuePair(LocationMessage.LAT, "" + lat), new NameValuePair(LocationMessage.LON, "" + lon), new NameValuePair(LocationMessage.TIME, "" + System.currentTimeMillis()), new NameValuePair(LocationMessage.SPEED, "15") }; method.setRequestBody(data);//from w w w . ja v a2s . c o m String response = executeHttpMethod(method); log.debug("Response: " + response); try { Configuration newConfiguration = new Configuration(response); log.debug("New min time interval: " + newConfiguration.getMinTimeInterval()); configuration.setMinTimeInterval(newConfiguration.getMinTimeInterval()); log.debug("Tracker count: " + newConfiguration.getTrackerCount()); } catch (IOException e) { log.error("Error parsing configuration", e); } }
From source file:net.jadler.JadlerMockingIntegrationTest.java
@Test public void havingEmptyBody() throws Exception { final PostMethod method = new PostMethod("http://localhost:" + port()); method.setRequestEntity(new StringRequestEntity("", null, null)); this.client.executeMethod(method); verifyThatRequest().havingBodyEqualTo("").havingBody(notNullValue()).havingBody(isEmptyString()) .receivedTimes(1);//w w w . j a va 2s. c o m }
From source file:com.urswolfer.intellij.plugin.gerrit.rest.GerritApiUtil.java
@NotNull private static HttpMethod doREST(@NotNull String host, @Nullable String login, @Nullable String password, @NotNull String path, @Nullable final String requestBody, final boolean post) throws IOException { HttpClient client = getHttpClient(login, password); String uri = host + path;/*from www. ja va 2 s . co m*/ return SslSupport.getInstance().executeSelfSignedCertificateAwareRequest(client, uri, new ThrowableConvertor<String, HttpMethod, IOException>() { @Override public HttpMethod convert(String uri) throws IOException { if (post) { PostMethod method = new PostMethod(uri); if (requestBody != null) { method.setRequestEntity( new StringRequestEntity(requestBody, "application/json", "UTF-8")); } return method; } return new GetMethod(uri); } }); }
From source file:com.denimgroup.threadfix.cli.HttpRestUtils.java
public String httpPostFile(String request, File file, String[] paramNames, String[] paramVals) { Protocol.registerProtocol("https", new Protocol("https", new AcceptAllTrustFactory(), 443)); PostMethod filePost = new PostMethod(request); filePost.setRequestHeader("Accept", "application/json"); try {/*from w w w.j a v a 2 s .com*/ Part[] parts = new Part[paramNames.length + 1]; parts[paramNames.length] = new FilePart("file", file); for (int i = 0; i < paramNames.length; i++) { parts[i] = new StringPart(paramNames[i], paramVals[i]); } filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams())); filePost.setContentChunked(true); HttpClient client = new HttpClient(); int status = client.executeMethod(filePost); if (status != 200) { System.err.println("Status was not 200."); } InputStream responseStream = filePost.getResponseBodyAsStream(); if (responseStream != null) { return IOUtils.toString(responseStream); } } catch (FileNotFoundException e1) { e1.printStackTrace(); } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return "There was an error and the POST request was not finished."; }
From source file:com.ebay.pulsar.collector.simulator.Simulator.java
private void init() { initList(m_ipList, ipFilePath);// ww w . j a v a2 s. c om initList(m_uaList, uaFilePath); initList(m_itemList, itmFilePath); initGUIDList(); String finalURL = ""; if (batchMode) { finalURL = BATCH_URL; } else { finalURL = URL; } m_payload = readFromResource(); HttpClientParams clientParams = new HttpClientParams(); clientParams.setSoTimeout(60000); m_client = new HttpClient(clientParams); m_method = new PostMethod(NODE + finalURL + EVENTTYPE); m_method.setRequestHeader("Connection", "Keep-Alive"); m_method.setRequestHeader("Accept-Charset", "UTF-8"); }
From source file:com.owncloud.android.lib.resources.notifications.RegisterAccountDeviceForProxyOperation.java
@Override protected RemoteOperationResult run(OwnCloudClient client) { RemoteOperationResult result = null; int status = -1; PostMethod post = null;/* w w w.j a v a 2s . co m*/ try { // Post Method String uriToPost = proxyUrl + PROXY_ROUTE; uriToPost += "?" + PUSH_TOKEN + "=" + URLEncoder.encode(pushToken) + "&"; uriToPost += DEVICE_IDENTIFIER + "=" + URLEncoder.encode(deviceIdentifier) + "&"; uriToPost += DEVICE_IDENTIFIER_SIGNATURE + "=" + URLEncoder.encode(deviceIdentifierSignature) + "&"; uriToPost += USER_PUBLIC_KEY + "=" + URLEncoder.encode(userPublicKey); post = new PostMethod(uriToPost); post.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); post.setRequestHeader(CONTENT_TYPE, FORM_URLENCODED); status = client.executeMethod(post); String response = post.getResponseBodyAsString(); if (isSuccess(status)) { result = new RemoteOperationResult(true, status, post.getResponseHeaders()); Log_OC.d(TAG, "Successful response: " + response); } else { result = new RemoteOperationResult(false, status, post.getResponseHeaders()); } } catch (Exception e) { result = new RemoteOperationResult(e); Log_OC.e(TAG, "Exception while registering device for notifications", e); } finally { if (post != null) { post.releaseConnection(); } } return result; }
From source file:com.boundary.sdk.event.notification.WebhookNotificationTest.java
@Test public void testNotification() throws InterruptedException, IOException { String body = readFile(NOTIFICATION_JSON, Charset.defaultCharset()); out.setExpectedMessageCount(1);//from w w w .j av a 2s . c o m CamelContext context = context(); RouteDefinition routeDefinition = context.getRouteDefinition("WEBHOOK-TEST"); assertNotNull("RouteDefinition is null", routeDefinition); List<FromDefinition> inputs = routeDefinition.getInputs(); FromDefinition from = inputs.get(0); String uri = from.getEndpointUri(); uri = uri.replaceFirst("jetty:", ""); LOG.debug("uri: {}", uri); // Send HTTP notification HttpClient httpclient = new HttpClient(); PostMethod httppost = new PostMethod(uri); Header contentHeader = new Header("Content-Type", "application/json"); httppost.setRequestHeader(contentHeader); StringRequestEntity reqEntity = new StringRequestEntity(body, null, null); httppost.setRequestEntity(reqEntity); int status = httpclient.executeMethod(httppost); assertEquals("Received wrong response status", 200, status); out.assertIsSatisfied(); List<Exchange> exchanges = out.getExchanges(); LOG.debug("EXCHANGE COUNT: {}", exchanges.size()); for (Exchange exchange : exchanges) { Message message = exchange.getIn(); String messageBody = message.getBody(String.class); Object o = message.getBody(); LOG.debug("class: " + o.getClass().toString()); LOG.debug("messageBody: " + messageBody); LOG.debug("id: " + exchange.getExchangeId()); //assertEquals("Body not equal",body,messageBody); } }
From source file:edu.northwestern.jcr.adapter.fedora.persistence.FedoraConnectorREST.java
/** * Sends an HTTP POST request and returns the status code. * * @param url URL of the service//from www . j a va 2 s. c o m * @return status code */ private int httpPost(String url) throws Exception { PostMethod postMethod = null; try { postMethod = new PostMethod(url); postMethod.setDoAuthentication(true); postMethod.getParams().setParameter("Connection", "Keep-Alive"); postMethod.setContentChunked(true); fc.getHttpClient().executeMethod(postMethod); return postMethod.getStatusCode(); } catch (Exception e) { String msg = "error connecting to the Fedora server"; log.error(msg); throw new RepositoryException(msg, null); } finally { if (postMethod != null) { postMethod.releaseConnection(); } } }