List of usage examples for org.apache.commons.httpclient.methods EntityEnclosingMethod setRequestBody
public void setRequestBody(String paramString)
From source file:com.manning.blogapps.chapter10.examples.AuthPost.java
public static void main(String[] args) throws Exception { if (args.length < 4) { System.out.println("USAGE: authpost <username> <password> <filepath> <url>"); System.exit(-1);/* ww w . j a v a 2 s.c o m*/ } String credentials = args[0] + ":" + args[1]; String filepath = args[2]; String url = args[3]; HttpClient httpClient = new HttpClient(); EntityEnclosingMethod method = new PostMethod(url); method.setRequestHeader("Authorization", "Basic " + new String(Base64.encodeBase64(credentials.getBytes()))); File upload = new File(filepath); method.setRequestHeader("Title", upload.getName()); method.setRequestBody(new FileInputStream(upload)); String contentType = "application/atom+xml; charset=utf8"; if (filepath.endsWith(".gif")) contentType = "image/gif"; else if (filepath.endsWith(".jpg")) contentType = "image/jpg"; method.setRequestHeader("Content-type", contentType); httpClient.executeMethod(method); System.out.println(method.getResponseBodyAsString()); }
From source file:com.manning.blogapps.chapter10.examples.AuthPut.java
public static void main(String[] args) throws Exception { if (args.length < 4) { System.out.println("USAGE: authput <username> <password> <filepath> <url>"); System.exit(-1);/*from w w w . jav a 2 s . c o m*/ } String credentials = args[0] + ":" + args[1]; String filepath = args[2]; String url = args[3]; HttpClient httpClient = new HttpClient(); EntityEnclosingMethod method = new PutMethod(url); method.setRequestHeader("Authorization", "Basic " + new String(Base64.encodeBase64(credentials.getBytes()))); File upload = new File(filepath); method.setRequestHeader("name", upload.getName()); String contentType = "application/atom+xml; charset=utf8"; if (filepath.endsWith(".gif")) contentType = "image/gif"; else if (filepath.endsWith(".jpg")) contentType = "image/jpg"; method.setRequestHeader("Content-type", contentType); method.setRequestBody(new FileInputStream(filepath)); httpClient.executeMethod(method); System.out.println(method.getResponseBodyAsString()); }
From source file:au.edu.usq.fascinator.access.couch.CouchAccessControl.java
private void put(JsonConfigHelper oldRecord, List<String> newRoles, String method) throws Exception { EntityEnclosingMethod rq = null; try {/*www .jav a 2 s .co m*/ // Prepare to send String dataToSend = writeUpdateString(oldRecord, newRoles); String recordId = oldRecord.get("_id"); // Get our connection ready if (method.equals("PUT")) { rq = new PutMethod(url + recordId); } else { rq = new PostMethod(url); } rq.addRequestHeader("Content-Type", "text/plain; charset=UTF-8"); rq.setRequestBody(dataToSend); int statusCode = couch.executeMethod(rq); // Valid responses are anywhere in 200 range for this if (statusCode < 200 || statusCode > 299) { throw new Exception( "Database " + method + " failed!: '" + statusCode + "': " + rq.getResponseBodyAsString()); } } catch (Exception ex) { throw ex; } finally { if (rq != null) { rq.releaseConnection(); } } }
From source file:org.chiba.xml.xforms.connector.http.AbstractHTTPConnector.java
/** * Performs a HTTP POST request./*w w w . ja v a 2s . c om*/ * * @param uri the request uri. * @param body the request body. * @param type the content type. * @throws XFormsException if any error occurred during the request. */ protected void post(String uri, String body, String type) throws XFormsException { try { EntityEnclosingMethod httpMethod = new PostMethod(uri); httpMethod.setRequestBody(body); httpMethod.setRequestHeader(new Header("Content-Type", type)); httpMethod.setRequestHeader(new Header("Content-Length", String.valueOf(body.length()))); httpMethod.setRequestHeader(new Header("User-Agent", ChibaBean.getAppInfo())); execute(httpMethod); } catch (Exception e) { throw new XFormsException(e); } }
From source file:org.chiba.xml.xforms.connector.http.AbstractHTTPConnector.java
/** * Performs a HTTP PUT request.//from w w w .jav a2s .c o m * * @param uri the request uri. * @param body the request body. * @param type the content type. * @throws XFormsException if any error occurred during the request. */ protected void put(String uri, String body, String type) throws XFormsException { try { EntityEnclosingMethod httpMethod = new PutMethod(uri); httpMethod.setRequestBody(body); httpMethod.setRequestHeader(new Header("Content-Type", type)); httpMethod.setRequestHeader(new Header("Content-Length", String.valueOf(body.length()))); httpMethod.setRequestHeader(new Header("User-Agent", ChibaBean.getAppInfo())); execute(httpMethod); } catch (Exception e) { throw new XFormsException(e); } }
From source file:org.entando.entando.plugins.jpoauthclient.aps.system.services.client.ProviderConnectionManager.java
private String invokePostPutMethod(String url, boolean isPost, String body, Properties parameters, MediaType mediaType) {// w ww. j av a 2 s. c o m String result = null; EntityEnclosingMethod method = null; try { HttpClient client = new HttpClient(); if (isPost) { method = new PostMethod(url); } else { method = new PutMethod(url); } this.addQueryString(method, parameters); method.setRequestBody(body); method.setRequestHeader("Content-type", mediaType.toString() + "; charset=UTF-8"); client.executeMethod(method); byte[] responseBody = method.getResponseBody(); result = new String(responseBody); } catch (Throwable t) { _logger.error("Error invoking Post or put Method", t); } finally { if (null != method) { method.releaseConnection(); } } return result; }
From source file:org.n52.wps.io.datahandler.generator.GeoServerUploader.java
private String sendRasterRequest(String target, String request, String method, String username, String password) throws HttpException, IOException { HttpClient client = new HttpClient(); EntityEnclosingMethod requestMethod = null; if (method.equalsIgnoreCase("POST")) { requestMethod = new PostMethod(target); requestMethod.setRequestHeader("Content-type", "application/xml"); }/*from w w w . jav a 2s .c o m*/ if (method.equalsIgnoreCase("PUT")) { requestMethod = new PutMethod(target); requestMethod.setRequestHeader("Content-type", "text/plain"); } requestMethod.setRequestBody(request); UsernamePasswordCredentials creds = new UsernamePasswordCredentials(username, password); client.getState().setCredentials(null, null, creds); int statusCode = client.executeMethod(requestMethod); if (!((statusCode == HttpStatus.SC_OK) || (statusCode == HttpStatus.SC_CREATED))) { System.err.println("Method failed: " + requestMethod.getStatusLine()); } // Read the response body. byte[] responseBody = requestMethod.getResponseBody(); return new String(responseBody); }
From source file:org.n52.wps.io.datahandler.generator.GeoServerUploader.java
private String sendShpRequest(String target, InputStream request, String method, String username, String password) throws HttpException, IOException { HttpClient client = new HttpClient(); EntityEnclosingMethod requestMethod = null; if (method.equalsIgnoreCase("POST")) { requestMethod = new PostMethod(target); requestMethod.setRequestHeader("Content-type", "text/xml"); }/*from ww w .j av a 2s . c o m*/ if (method.equalsIgnoreCase("PUT")) { requestMethod = new PutMethod(target); requestMethod.setRequestHeader("Content-type", "application/zip"); } requestMethod.setRequestBody(request); UsernamePasswordCredentials creds = new UsernamePasswordCredentials(username, password); client.getState().setCredentials(null, null, creds); int statusCode = client.executeMethod(requestMethod); if (!((statusCode == HttpStatus.SC_OK) || (statusCode == HttpStatus.SC_CREATED))) { System.err.println("Method failed: " + requestMethod.getStatusLine()); } // Read the response body. byte[] responseBody = requestMethod.getResponseBody(); return new String(responseBody); }
From source file:org.obm.caldav.client.AbstractPushTest.java
@SuppressWarnings("deprecation") private void appendBody(EntityEnclosingMethod hm, ByteArrayOutputStream out) { hm.setRequestBody(new ByteArrayInputStream(out.toByteArray())); }