List of usage examples for org.apache.http.client.methods HttpPut HttpPut
public HttpPut(final String uri)
From source file:com.serena.rlc.provider.servicenow.client.ApprovalWaiter.java
synchronized void notifyRLC(String status) { try {//from www. java 2 s .co m String uri = callbackUrl + executionId + "/" + status; logger.debug("Start executing RLC PUT request to url=\"{}\"", uri); DefaultHttpClient rlcParams = new DefaultHttpClient(); HttpPut put = new HttpPut(uri); UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(callbackUsername, callbackPassword); put.addHeader(BasicScheme.authenticate(credentials, "US-ASCII", false)); //put.addHeader("Content-Type", "application/x-www-form-urlencoded"); //put.addHeader("Accept", "application/json"); logger.info(credentials.toString()); HttpResponse response = rlcParams.execute(put); if (response.getStatusLine().getStatusCode() != 200) { logger.error("HTTP Status Code: " + response.getStatusLine().getStatusCode()); } } catch (IOException ex) { logger.error(ex.getLocalizedMessage()); } }
From source file:com.tek271.reverseProxy.servlet.ProxyFilter.java
/** * Helper method for passing post-requests */// ww w . j a va2 s .c o m @SuppressWarnings({ "JavaDoc" }) private static HttpUriRequest createNewRequest(HttpServletRequest request, String newUrl) throws IOException { String method = request.getMethod(); if (method.equals("POST")) { HttpPost httppost = new HttpPost(newUrl); if (ServletFileUpload.isMultipartContent(request)) { MultipartEntity entity = getMultipartEntity(request); httppost.setEntity(entity); addCustomHeaders(request, httppost, "Content-Type"); } else { StringEntity entity = getEntity(request); httppost.setEntity(entity); addCustomHeaders(request, httppost); } return httppost; } else if (method.equals("PUT")) { StringEntity entity = getEntity(request); HttpPut httpPut = new HttpPut(newUrl); httpPut.setEntity(entity); addCustomHeaders(request, httpPut); return httpPut; } else if (method.equals("DELETE")) { StringEntity entity = getEntity(request); HttpDeleteWithBody httpDelete = new HttpDeleteWithBody(newUrl); httpDelete.setEntity(entity); addCustomHeaders(request, httpDelete); return httpDelete; } else { HttpGet httpGet = new HttpGet(newUrl); addCustomGetHeaders(request, httpGet); return httpGet; } }
From source file:com.citrus.mobile.RESTclient.java
public JSONObject makePutrequest(JSONObject details) { HttpClient client = new DefaultHttpClient(); HttpPut put = null;/*from w w w . j a v a 2s. com*/ try { put = new HttpPut(urls.getString(base_url) + urls.getString(type)); } catch (JSONException e) { e.printStackTrace(); } Iterator<String> iterhead = headers.keys(); while (iterhead.hasNext()) { String key = iterhead.next(); try { String value = headers.getString(key); put.addHeader(key, value); } catch (JSONException e) { Log.d("exception", e.toString()); } } try { put.setEntity(new StringEntity(details.toString())); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } try { response = client.execute(put); } catch (IOException e) { e.printStackTrace(); } return parseResponse(response); }
From source file:org.fashiontec.bodyapps.sync.Sync.java
/** * Manages put requests/*w ww . j av a 2s . c o m*/ * * @param url * @param json * @param conTimeOut * @param socTimeOut * @return */ public HttpResponse put(String url, String json, int conTimeOut, int socTimeOut) { HttpResponse response = null; try { HttpParams httpParameters = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParameters, conTimeOut); HttpConnectionParams.setSoTimeout(httpParameters, socTimeOut); HttpClient client = new DefaultHttpClient(httpParameters); HttpPut request = new HttpPut(url); StringEntity se = new StringEntity(json); request.setEntity(se); request.setHeader("Accept", "application/json"); request.setHeader("Content-type", "application/json"); response = client.execute(request); } catch (Exception e) { Log.e(TAG, e.getMessage()); } return response; }
From source file:com.amazonaws.http.HttpRequestFactory.java
/** * Creates an HttpClient method object based on the specified request and * populates any parameters, headers, etc. from the original request. * * @param request/*from w ww . j av a 2 s . co m*/ * The request to convert to an HttpClient method object. * @param context * The execution context of the HTTP method to be executed * * @return The converted HttpClient method object with any parameters, * headers, etc. from the original request set. * @throws FakeIOException only for test simulation */ HttpRequestBase createHttpRequest(Request<?> request, ClientConfiguration clientConfiguration, ExecutionContext context) throws FakeIOException { URI endpoint = request.getEndpoint(); /* * HttpClient cannot handle url in pattern of "http://host//path", so we * have to escape the double-slash between endpoint and resource-path * into "/%2F" */ String uri = HttpUtils.appendUri(endpoint.toString(), request.getResourcePath(), true); String encodedParams = HttpUtils.encodeParameters(request); /* * For all non-POST requests, and any POST requests that already have a * payload, we put the encoded params directly in the URI, otherwise, * we'll put them in the POST request's payload. */ boolean requestHasNoPayload = request.getContent() != null; boolean requestIsPost = request.getHttpMethod() == HttpMethodName.POST; boolean putParamsInUri = !requestIsPost || requestHasNoPayload; if (encodedParams != null && putParamsInUri) { uri += "?" + encodedParams; } HttpRequestBase httpRequest; if (request.getHttpMethod() == HttpMethodName.POST) { HttpPost postMethod = new HttpPost(uri); /* * If there isn't any payload content to include in this request, * then try to include the POST parameters in the query body, * otherwise, just use the query string. For all AWS Query services, * the best behavior is putting the params in the request body for * POST requests, but we can't do that for S3. */ if (request.getContent() == null && encodedParams != null) { postMethod.setEntity(newStringEntity(encodedParams)); } else { postMethod.setEntity(new RepeatableInputStreamRequestEntity(request)); } httpRequest = postMethod; } else if (request.getHttpMethod() == HttpMethodName.PUT) { HttpPut putMethod = new HttpPut(uri); httpRequest = putMethod; /* * Enable 100-continue support for PUT operations, since this is * where we're potentially uploading large amounts of data and want * to find out as early as possible if an operation will fail. We * don't want to do this for all operations since it will cause * extra latency in the network interaction. */ putMethod.getParams().setParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, true); /* * We should never reuse the entity of the previous request, since * reading from the buffered entity will bypass reading from the * original request content. And if the content contains InputStream * wrappers that were added for validation-purpose (e.g. * Md5DigestCalculationInputStream), these wrappers would never be * read and updated again after AmazonHttpClient resets it in * preparation for the retry. Eventually, these wrappers would * return incorrect validation result. */ if (request.getContent() != null) { HttpEntity entity = new RepeatableInputStreamRequestEntity(request); if (request.getHeaders().get("Content-Length") == null) { entity = newBufferedHttpEntity(entity); } putMethod.setEntity(entity); } } else if (request.getHttpMethod() == HttpMethodName.PATCH) { HttpPatch patchMethod = new HttpPatch(uri); httpRequest = patchMethod; /* * We should never reuse the entity of the previous request, since * reading from the buffered entity will bypass reading from the * original request content. And if the content contains InputStream * wrappers that were added for validation-purpose (e.g. * Md5DigestCalculationInputStream), these wrappers would never be * read and updated again after AmazonHttpClient resets it in * preparation for the retry. Eventually, these wrappers would * return incorrect validation result. */ if (request.getContent() != null) { HttpEntity entity = new RepeatableInputStreamRequestEntity(request); if (request.getHeaders().get("Content-Length") == null) { entity = newBufferedHttpEntity(entity); } patchMethod.setEntity(entity); } } else if (request.getHttpMethod() == HttpMethodName.GET) { httpRequest = new HttpGet(uri); } else if (request.getHttpMethod() == HttpMethodName.DELETE) { httpRequest = new HttpDelete(uri); } else if (request.getHttpMethod() == HttpMethodName.HEAD) { httpRequest = new HttpHead(uri); } else { throw new AmazonClientException("Unknown HTTP method name: " + request.getHttpMethod()); } configureHeaders(httpRequest, request, context, clientConfiguration); return httpRequest; }
From source file:org.eclipse.mylyn.internal.bugzilla.rest.core.BugzillaRestAuthenticatedPutRequest.java
@Override protected HttpRequestBase createHttpRequestBase() { String bugUrl = getUrlSuffix(); LoginToken token = ((BugzillaRestHttpClient) getClient()).getLoginToken(); if (token != null && bugUrl.length() > 0) { if (bugUrl.endsWith("?")) { //$NON-NLS-1$ bugUrl += ("token=" + token.getToken()); //$NON-NLS-1$ } else {//from w w w .j a va 2s . c o m bugUrl += ("&token=" + token.getToken()); //$NON-NLS-1$ } } HttpPut request = new HttpPut(baseUrl() + bugUrl); request.setHeader(CONTENT_TYPE, APPLICATION_JSON); request.setHeader(ACCEPT, APPLICATION_JSON); return request; }
From source file:com.foundationdb.http.CsrfProtectionITBase.java
@Test public void putBlockedWithMissingReferer() throws Exception { HttpUriRequest request = new HttpPut(defaultURI()); response = client.execute(request);//from w w w . ja v a 2 s .co m assertEquals("status", HttpStatus.SC_FORBIDDEN, response.getStatusLine().getStatusCode()); assertThat("reason", response.getStatusLine().getReasonPhrase(), containsString("Referer")); }
From source file:com.epam.ngb.cli.manager.command.handler.http.AbstractAnnotationReferenceHandler.java
@Override public int runCommand() { String url = serverParameters.getServerUrl() + getRequestUrl(); for (Long annotationFileId : annotationFileIds) { try {/*w w w. j a va 2s .c o m*/ URIBuilder builder = new URIBuilder(String.format(url, referenceId)); builder.addParameter("annotationFileId", String.valueOf(annotationFileId)); builder.addParameter("remove", String.valueOf(isRemoving())); HttpPut put = new HttpPut(builder.build()); setDefaultHeader(put); if (isSecure()) { addAuthorizationToRequest(put); } String result = RequestManager.executeRequest(put); checkAndPrintRegistrationResult(result, printJson, printTable); } catch (URISyntaxException e) { throw new ApplicationException(e.getMessage(), e); } } return 0; }
From source file:org.graphwalker.restful.RestTest.java
@Override public void e_SetData() { response = httpExecute(new HttpPut("http://localhost:9191/graphwalker/setData/MAX_BOOKS=6;")); }
From source file:db.dao.Dao.java
public HttpResponse update(String url, Object entity) { url = checkIfLocal(url);/*ww w . j a va 2 s. co m*/ HttpClient client = HttpClientBuilder.create().build(); HttpPut putRequest = new HttpPut(url); String usernamepassword = "Pepijn:Mores"; String encoded = Base64.encodeBase64String(usernamepassword.getBytes()); putRequest.setHeader("content-type", "application/json"); putRequest.setHeader("Authorization", encoded); ; String entityToJson = null; try { ObjectMapper mapper = new ObjectMapper(); entityToJson = mapper.writeValueAsString(entity); } catch (JsonProcessingException ex) { Logger.getLogger(ArticleDao.class.getName()).log(Level.SEVERE, null, ex); } StringEntity jsonString = null; try { jsonString = new StringEntity(entityToJson); } catch (UnsupportedEncodingException ex) { Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex); } putRequest.setEntity(jsonString); HttpResponse response = null; try { response = client.execute(putRequest); } catch (IOException ex) { Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex); } return response; }