List of usage examples for org.apache.http.client.methods HttpPut HttpPut
public HttpPut()
From source file:com.marketplace.io.Sender.java
/** * Constructs a <code>Sender</code> *//* w w w . ja va 2 s .c o m*/ public Sender() { this.httpPut = new HttpPut(); this.httpPost = new HttpPost(); this.httpClient = new DefaultHttpClient(); this.httpClient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); HttpConnectionParams.setConnectionTimeout(this.httpClient.getParams(), 20000); }
From source file:com.appdynamics.demo.gasp.service.RESTIntentService.java
@Override protected void onHandleIntent(Intent intent) { Uri action = intent.getData();/*from w w w . j ava 2 s . c o m*/ Bundle extras = intent.getExtras(); if (extras == null || action == null || !extras.containsKey(EXTRA_RESULT_RECEIVER)) { Log.e(TAG, "You did not pass extras or data with the Intent."); return; } int verb = extras.getInt(EXTRA_HTTP_VERB, GET); Bundle params = extras.getParcelable(EXTRA_PARAMS); Bundle headers = extras.getParcelable(EXTRA_HEADERS); ResultReceiver receiver = extras.getParcelable(EXTRA_RESULT_RECEIVER); try { HttpRequestBase request = null; // Get query params from Bundle and build URL switch (verb) { case GET: { request = new HttpGet(); attachUriWithQuery(request, action, params); } break; case DELETE: { request = new HttpDelete(); attachUriWithQuery(request, action, params); } break; case POST: { request = new HttpPost(); request.setURI(new URI(action.toString())); HttpPost postRequest = (HttpPost) request; if (params != null) { UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(paramsToList(params)); postRequest.setEntity(formEntity); } } break; case PUT: { request = new HttpPut(); request.setURI(new URI(action.toString())); HttpPut putRequest = (HttpPut) request; if (params != null) { UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(paramsToList(params)); putRequest.setEntity(formEntity); } } break; } // Get Headers from Bundle for (BasicNameValuePair header : paramsToList(headers)) { request.setHeader(header.getName(), header.getValue()); } if (request != null) { HttpClient client = new DefaultHttpClient(); Log.d(TAG, "Executing request: " + verbToString(verb) + ": " + action.toString()); HttpResponse response = client.execute(request); HttpEntity responseEntity = response.getEntity(); StatusLine responseStatus = response.getStatusLine(); int statusCode = responseStatus != null ? responseStatus.getStatusCode() : 0; if ((responseEntity != null) && (responseStatus.getStatusCode() == 200)) { Bundle resultData = new Bundle(); resultData.putString(REST_RESULT, EntityUtils.toString(responseEntity)); receiver.send(statusCode, resultData); } else { receiver.send(statusCode, null); } } } catch (URISyntaxException e) { Log.e(TAG, "URI syntax was incorrect. " + verbToString(verb) + ": " + action.toString(), e); receiver.send(0, null); } catch (UnsupportedEncodingException e) { Log.e(TAG, "A UrlEncodedFormEntity was created with an unsupported encoding.", e); receiver.send(0, null); } catch (ClientProtocolException e) { Log.e(TAG, "There was a problem when sending the request.", e); receiver.send(0, null); } catch (IOException e) { Log.e(TAG, "There was a problem when sending the request.", e); receiver.send(0, null); } catch (Exception e) { e.printStackTrace(); } }
From source file:org.modeshape.web.jcr.rest.client.http.HttpClientConnection.java
/** * @param server the server with the host and port information (never <code>null</code>) * @param url the URL that will be used in the request (never <code>null</code>) * @param method the HTTP request method (never <code>null</code>) * @throws Exception if there is a problem establishing the connection *///from w w w . ja va2 s . com public HttpClientConnection(Server server, URL url, RequestMethod method) throws Exception { assert server != null; assert url != null; assert method != null; this.httpClient = new DefaultHttpClient(); this.httpClient.getCredentialsProvider().setCredentials(new AuthScope(url.getHost(), url.getPort()), new UsernamePasswordCredentials(server.getUser(), server.getPassword())); // determine the request type if (RequestMethod.GET == method) { this.request = new HttpGet(); } else if (RequestMethod.DELETE == method) { this.request = new HttpDelete(); } else if (RequestMethod.POST == method) { this.request = new HttpPost(); } else if (RequestMethod.PUT == method) { this.request = new HttpPut(); } else { throw new RuntimeException(unknownHttpRequestMethodMsg.text(method)); } //set the accepts header to application/json this.request.setHeader("Accept", MediaType.APPLICATION_JSON); // set request URI this.request.setURI(url.toURI()); }
From source file:com.sap.core.odata.fit.ref.AbstractRefTest.java
protected HttpResponse callUri(final ODataHttpMethod httpMethod, final String uri, final String additionalHeader, final String additionalHeaderValue, final String requestBody, final String requestContentType, final HttpStatusCodes expectedStatusCode) throws Exception { HttpRequestBase request = httpMethod == ODataHttpMethod.GET ? new HttpGet() : httpMethod == ODataHttpMethod.DELETE ? new HttpDelete() : httpMethod == ODataHttpMethod.POST ? new HttpPost() : httpMethod == ODataHttpMethod.PUT ? new HttpPut() : new HttpPatch(); request.setURI(URI.create(getEndpoint() + uri)); if (additionalHeader != null) { request.addHeader(additionalHeader, additionalHeaderValue); }//from ww w. j a v a 2 s. c o m if (requestBody != null) { ((HttpEntityEnclosingRequest) request).setEntity(new StringEntity(requestBody)); request.setHeader(HttpHeaders.CONTENT_TYPE, requestContentType); } final HttpResponse response = getHttpClient().execute(request); assertNotNull(response); assertEquals(expectedStatusCode.getStatusCode(), response.getStatusLine().getStatusCode()); if (expectedStatusCode == HttpStatusCodes.OK) { assertNotNull(response.getEntity()); assertNotNull(response.getEntity().getContent()); } else if (expectedStatusCode == HttpStatusCodes.CREATED) { assertNotNull(response.getEntity()); assertNotNull(response.getEntity().getContent()); assertNotNull(response.getFirstHeader(HttpHeaders.LOCATION)); } else if (expectedStatusCode == HttpStatusCodes.NO_CONTENT) { assertTrue(response.getEntity() == null || response.getEntity().getContent() == null); } return response; }