/** * Create a Commons HttpMethodBase object for the given HTTP method and URI specification. * @param httpMethod the HTTP method * @param uri the URI * @return the Commons HttpMethodBase object */ protected HttpUriRequest createHttpUriRequest(HttpMethod httpMethod, URI uri) { switch (httpMethod) { case GET: return new HttpGet(uri); case HEAD: return new HttpHead(uri); case POST: return new HttpPost(uri); case PUT: return new HttpPut(uri); case PATCH: return new HttpPatch(uri); case DELETE: return new HttpDelete(uri); case OPTIONS: return new HttpOptions(uri); case TRACE: return new HttpTrace(uri); default: throw new IllegalArgumentException("Invalid HTTP method: " + httpMethod); } }
public HttpResponse doPut(String query, String username, String password, String body) { HttpPut httpPut = new HttpPut(context + query); httpPut.addHeader("content-type", "application/json"); httpPut.setEntity(new StringEntity(body, StandardCharsets.UTF_8)); return doRequest(httpPut, username, password); }
public TransactionResultEntity extendTransaction(final String transactionUrl) throws IOException { logger.debug("Sending extendTransaction request to transactionUrl: {}", transactionUrl); final HttpPut put = createPut(transactionUrl); put.setHeader("Accept", "application/json"); put.setHeader(HttpHeaders.PROTOCOL_VERSION, String.valueOf(transportProtocolVersionNegotiator.getVersion())); setHandshakeProperties(put); try (final CloseableHttpResponse response = getHttpClient().execute(put)) { final int responseCode = response.getStatusLine().getStatusCode(); logger.debug("extendTransaction responseCode={}", responseCode); try (final InputStream content = response.getEntity().getContent()) { switch (responseCode) { case RESPONSE_CODE_OK: return readResponse(content); default: throw handleErrResponse(responseCode, content); } } } }
public void append(String content) throws IOException { HttpPut putMethod = new HttpPut(consoleUri); try { LOGGER.debug("Appending console to URL -> {}", consoleUri); StringEntity entity = new StringEntity(content, charset); putMethod.setEntity(entity); HttpService.setSizeHeader(putMethod, entity.getContentLength()); CloseableHttpResponse response = httpService.execute(putMethod); LOGGER.debug("Got {}", response.getStatusLine().getStatusCode()); } finally { putMethod.releaseConnection(); } } }
parsedContentType = ContentType.parse(contentType); if (body != null && body.length() > 0) requestBody = new StringEntity(body, parsedContentType); request = new HttpGet(theURI); } else if (HttpPost.METHOD_NAME.equalsIgnoreCase(httpVerb)) { HttpPost postRequest = new HttpPost(theURI); if (requestBody != null) postRequest.setEntity(requestBody); request = postRequest; } else if (HttpPut.METHOD_NAME.equalsIgnoreCase(httpVerb)) { HttpPut putRequest = new HttpPut(theURI); if (requestBody != null) putRequest.setEntity(requestBody); request = putRequest; } else request = new HttpGet(theURI);
HttpPost postRequest = new HttpPost(request.getUrl()); postRequest.addHeader(HEADER_CONTENT_TYPE, request.getPostBodyContentType()); HttpEntity entity; entity = new ByteArrayEntity(postBody); postRequest.setEntity(entity); return postRequest; } else { return new HttpGet(request.getUrl()); return new HttpGet(request.getUrl()); case Method.DELETE: return new HttpDelete(request.getUrl()); case Method.POST: { HttpPost postRequest = new HttpPost(request.getUrl()); HttpPut putRequest = new HttpPut(request.getUrl()); putRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType()); setEntityIfNonEmptyBody(putRequest, request); return putRequest; return new HttpHead(request.getUrl()); case Method.OPTIONS: return new HttpOptions(request.getUrl()); case Method.TRACE: return new HttpTrace(request.getUrl());
URI uri = new URI(sb.toString()); if (method instanceof HttpPut) { HttpPut put = new HttpPut(uri); put.setEntity(((HttpPut) method).getEntity()); put.setHeaders(method.getAllHeaders()); method = put; } else if (method instanceof HttpGet) { method = new HttpGet(uri); } else if (method instanceof HttpHead) { method = new HttpHead(uri); } else if (method instanceof HttpDelete) { method = new HttpDelete(uri); } else if (method instanceof HttpPost) { HttpPost post = new HttpPost(uri); post.setEntity(((HttpPost) method).getEntity()); post.setHeaders(method.getAllHeaders()); method = post;
HttpResponse response = client.execute(new HttpGet(uploadUrl)); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == 404) { request = new HttpPut(uploadUrl); System.out.println("Table " + tableName + " exists, will update its configuration."); } else { request = new HttpPost(createUrl); System.out.println("Table " + tableName + " does not exist, creating."); request.setEntity(new StringEntity(computedConfig, Charsets.UTF_8)); response = client.execute(request); statusCode = response.getStatusLine().getStatusCode();
String method = request.getMethod(); if ("POST".equals(method)) { HttpPost postRequest = new HttpPost(request.getUri()); if (request.getContent() != null) { postRequest.setEntity(new InputStreamEntity(request.getContent(), request.getContentLength())); httpRequest = new HttpGet(request.getUri()); } else if ("PUT".equals(method)) { HttpPut putRequest = new HttpPut(request.getUri()); if (request.getContent() != null) { putRequest.setEntity(new InputStreamEntity(request.getContent(), request.getContentLength())); httpRequest = new HttpDelete(request.getUri()); } else if ("HEAD".equals(method)) { httpRequest = new HttpHead(request.getUri()); } else { throw new UnsupportedOperationException("Unsupported method: " + method);
private HttpRequestBase constructRequestBase(HttpEntity theEntity) { String url = myUrl.toString(); switch (myRequestType) { case DELETE: return new HttpDelete(url); case PATCH: HttpPatch httpPatch = new HttpPatch(url); httpPatch.setEntity(theEntity); return httpPatch; case OPTIONS: return new HttpOptions(url); case POST: HttpPost httpPost = new HttpPost(url); httpPost.setEntity(theEntity); return httpPost; case PUT: HttpPut httpPut = new HttpPut(url); httpPut.setEntity(theEntity); return httpPut; case GET: default: return new HttpGet(url); } }
+ CouchDBConstants.URL_SEPARATOR + _id, null, null); HttpPut put = new HttpPut(uri); HttpGet get = new HttpGet(uri); get.addHeader("Accept", "application/json"); response = httpClient.execute(httpHost, get, CouchDBUtils.getContext(httpHost)); stringEntity = new StringEntity(object.toString(), Constants.CHARSET_UTF8); stringEntity.setContentType("application/json"); put.setEntity(stringEntity); response = httpClient.execute(httpHost, put, CouchDBUtils.getContext(httpHost));
private HttpRequest createRequest(RestConfig configuration, LogEventDto dto) throws URISyntaxException { String body = buildRequestBody(configuration, dto); ContentType contentType = buildContentType(configuration); StringEntity entity = new StringEntity(body, contentType); final HttpEntityEnclosingRequestBase request; if (configuration.getMethod() == MethodType.POST) { request = new HttpPost(targetUri); } else { request = new HttpPut(targetUri); } request.setEntity(entity); return request; }
synchronized private void doRegister(String service) { String url = registryURL; try { HttpPut method = new HttpPut(url); method.addHeader("service", service); ResponseHandler<String> handler = new BasicResponseHandler(); String responseBody = httpClient.execute(method, handler); LOG.debug("PUT to " + url + " got a " + responseBody); } catch (Exception e) { LOG.debug("PUT to " + url + " failed with: " + e); } }
public void wipeCache(String entity, String event, String cacheKey) throws IOException { HttpPut request; String url; if (cacheKey.contains("/")) { url = baseUrl + "/cache/" + entity + "/" + event; request = new HttpPut(url); request.setEntity(new StringEntity(cacheKey, ContentType.create(APPLICATION_JSON, UTF_8))); } else { url = baseUrl + "/cache/" + entity + "/" + cacheKey + "/" + event; request = new HttpPut(url); } HttpResponse response = null; try { response = client.execute(request); if (response.getStatusLine().getStatusCode() != 200) { String msg = EntityUtils.toString(response.getEntity()); throw new IOException(INVALID_RESPONSE + response.getStatusLine().getStatusCode() + " with cache wipe url " + url + "\n" + msg); } } finally { cleanup(request, response); } }
@Test public void test() throws Exception { try (CloseableHttpClient client = HttpClientBuilder.create().build()) { HttpPut put = new HttpPut( String.format(URL_TEMPLCATE, PORT, TABLE_NAME.getNameAsString(), ROW, FAMILY, QUALIFIER)); put.setEntity(EntityBuilder.create().setText(VALUE) .setContentType(ContentType.create("text-plain", StandardCharsets.UTF_8)).build()); try (CloseableHttpResponse resp = client.execute(put)) { assertEquals(HttpStatus.SC_OK, resp.getStatusLine().getStatusCode()); } HttpGet get = new HttpGet( String.format(URL_TEMPLCATE, PORT, TABLE_NAME.getNameAsString(), ROW, FAMILY, QUALIFIER)); try (CloseableHttpResponse resp = client.execute(get)) { assertEquals(HttpStatus.SC_OK, resp.getStatusLine().getStatusCode()); assertEquals("value", Bytes.toString(ByteStreams.toByteArray(resp.getEntity().getContent()))); } get = new HttpGet(String.format(URL_TEMPLCATE, PORT, TABLE_NAME.getNameAsString(), "whatever", FAMILY, QUALIFIER)); try (CloseableHttpResponse resp = client.execute(get)) { assertEquals(HttpStatus.SC_NOT_FOUND, resp.getStatusLine().getStatusCode()); } } } }
httpUriRequest = new HttpPost(url); log.debug("POST method created based on client request"); } else if (methodName.equalsIgnoreCase("PUT")) { httpUriRequest = new HttpPut(url); log.debug("PUT method created based on client request"); } else if (methodName.equalsIgnoreCase("DELETE")) { log.debug("GET method created based on client request"); } else if (methodName.equalsIgnoreCase("HEAD")) { httpUriRequest = new HttpHead(url); log.debug("HEAD method created based on client request");
private boolean setCache(boolean flag) throws IOException { String url = baseUrl + "/admin/config"; HttpPut put = newPut(url); HttpResponse response = null; try { HashMap<String, String> paraMap = new HashMap<String, String>(); paraMap.put("key", "kylin.query.cache-enabled"); paraMap.put("value", flag + ""); put.setEntity(new StringEntity(new ObjectMapper().writeValueAsString(paraMap), UTF_8)); response = client.execute(put); EntityUtils.consume(response.getEntity()); if (response.getStatusLine().getStatusCode() != 200) { return false; } else { return true; } } finally { cleanup(put, response); } }
URL url = new URL("http://www.example.com/resource"); HttpClient client = new DefaultHttpClient(); HttpPut put= new HttpPut(url); List<NameValuePair> pairs = new ArrayList<NameValuePair>(); pairs.add(new BasicNameValuePair("key1", "value1")); pairs.add(new BasicNameValuePair("key2", "value2")); put.setEntity(new UrlEncodedFormEntity(pairs)); HttpResponse response = client.execute(put);