PutMethod put = new PutMethod("http://jakarta.apache.org"); put.setRequestBody(new FileInputStream("UploadMe.gif")); // execute the method and handle any error responses. ... // Handle the response. Note that a successful response may not be // 200, but may also be 201 Created, 204 No Content or any of the other // 2xx range responses.
/** * put-method for rest interface to couchdb * @author daemonmidi@gmail.com * @since version 2.5 * @param url * @return string responesBody */ private String sendPut(String url, String requestBody){ String responseBody = ""; org.apache.commons.httpclient.HttpClient client = new HttpClient(); client.getParams().setParameter("http.useragent", "jbrofuzz"); PutMethod method = new PutMethod(url); // method.getParams().setParameter("retryHandler", new DefaultHttpRequestRetryHandler(3, false)); if (requestBody.length() > 0 ) method.setRequestBody(requestBody); try { responseBody = method.getResponseBodyAsString(); } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally{ client.getHttpConnectionManager().closeIdleConnections(0); } return responseBody; }
post.setRequestBody(bais); try { int status = client.executeMethod(post);
protected void put(String path, NameValuePair[] data) { PutMethod req = new PutMethod(path); req.setRequestBody(EncodingUtil.formUrlEncode(data, "utf-8")); req.getParams().setContentCharset("utf-8"); send(req); }
/** * This method implements the HTTP Put Method * * @param uri Resource URI * @param requestPayload Content which has to be put into the Resource * @return ResponseCode of HTTP Interaction */ @SuppressWarnings("deprecation") public static HttpResponseMessage Put(String uri, String requestPayload, String acceptHeaderValue) { PutMethod method = new PutMethod(uri); // requestPayload = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + // requestPayload; HighLevelRestApi.setAcceptHeader(method, acceptHeaderValue); method.setRequestBody(requestPayload); HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method); // kill <?xml... in front of response HighLevelRestApi.cleanResponseBody(responseMessage); return responseMessage; }
/** * Execute the PUT method for the given path. * * @param path the server relative path to put the data * @param data The byte array. * @return true if the method is succeeded. * @exception HttpException * @exception IOException */ public boolean putMethod(String path, byte[] data) throws HttpException, IOException { setClient(); PutMethod method = new PutMethod(URIUtil.encodePathQuery(path)); generateIfHeader(method); if (getGetContentType() != null && !getGetContentType().equals("")) method.setRequestHeader("Content-Type", getGetContentType()); method.setRequestHeader("Content-Length", String.valueOf(data.length)); method.setRequestBody(new ByteArrayInputStream(data)); generateTransactionHeader(method); int statusCode = client.executeMethod(method); setStatusCode(statusCode); return (statusCode >= 200 && statusCode < 300) ? true : false; }
/** * Execute the PUT method for the given path from the given url. * * @param path the server relative path to put the data * @param url The URL to get a resource. * @return true if the method is succeeded. * @exception HttpException * @exception IOException */ public boolean putMethod(String path, URL url) throws HttpException, IOException { setClient(); PutMethod method = new PutMethod(URIUtil.encodePathQuery(path)); generateIfHeader(method); if (getGetContentType() != null && !getGetContentType().equals("")) method.setRequestHeader("Content-Type", getGetContentType()); method.setRequestBody(url.openStream()); generateTransactionHeader(method); int statusCode = client.executeMethod(method); setStatusCode(statusCode); return (statusCode >= 200 && statusCode < 300) ? true : false; }
/** * Execute the PUT method for the given path. * * @param path the server relative path to put the given file * @param file the filename to get on local. * @return true if the method is succeeded. * @exception HttpException * @exception IOException */ public boolean putMethod(String path, File file) throws HttpException, IOException { setClient(); PutMethod method = new PutMethod(URIUtil.encodePathQuery(path)); generateIfHeader(method); if (getGetContentType() != null && !getGetContentType().equals("")) method.setRequestHeader("Content-Type", getGetContentType()); long fileLength = file.length(); method.setRequestContentLength(fileLength <= Integer.MAX_VALUE ? (int) fileLength : PutMethod.CONTENT_LENGTH_CHUNKED); method.setRequestBody(new FileInputStream(file)); generateTransactionHeader(method); int statusCode = client.executeMethod(method); setStatusCode(statusCode); return (statusCode >= 200 && statusCode < 300) ? true : false; }
@Test public void test() { HttpClient loggedClient = new HttpClient(); PostMethod loginPostMethod = new PostMethod(loginURL); loginPostMethod.addParameter("j_username", login); loginPostMethod.addParameter("j_password", password); loginPostMethod.addParameter("remember", "1"); loginPostMethod.addParameter("clientType", clientType); loginPostMethod.addParameter("clientVersion", clientVersion); httpClient.executeMethod(postMethod); String USERS_URL = HTTP_SERVER_DOMAIN + "/service"; PutMethod put = new PutMethod(USERS_URL); put.addRequestHeader("Content-Type", "application/json"); put.setRequestBody("your_body"); try { loggedClient.executeMethod(put); } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } assertEquals(HttpStatus.SC_OK, put.getStatusCode()); }
@Test public void test() { HttpClient loggedClient = new HttpClient(); PostMethod loginPostMethod = new PostMethod(loginURL); loginPostMethod.addParameter("j_username", login); loginPostMethod.addParameter("j_password", password); loginPostMethod.addParameter("remember", "1"); loginPostMethod.addParameter("clientType", clientType); loginPostMethod.addParameter("clientVersion", clientVersion); httpClient.executeMethod(postMethod); String USERS_URL = HTTP_SERVER_DOMAIN + "/service"; PutMethod put = new PutMethod(USERS_URL); put.addRequestHeader("Content-Type", "application/json"); put.setRequestBody("your_body"); try { loggedClient.executeMethod(put); } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } assertEquals(HttpStatus.SC_OK, put.getStatusCode());
/** * Execute the PUT method for the given path. * * @param path the server relative path to put the data * @param data String to send. * @return true if the method is succeeded. * @exception HttpException * @exception IOException */ public boolean putMethod(String path, String data) throws HttpException, IOException { setClient(); PutMethod method = new PutMethod(URIUtil.encodePathQuery(path)); generateIfHeader(method); if (getGetContentType() != null && !getGetContentType().equals("")) method.setRequestHeader("Content-Type", getGetContentType()); method.setRequestBody(data); generateTransactionHeader(method); int statusCode = client.executeMethod(method); setStatusCode(statusCode); return (statusCode >= 200 && statusCode < 300) ? true : false; }
/** * Execute the PUT method for the given path. * * @param path the server relative path to put the data * @param is The input stream. * @return true if the method is succeeded. * @exception HttpException * @exception IOException */ public boolean putMethod(String path, InputStream is) throws HttpException, IOException { setClient(); PutMethod method = new PutMethod(URIUtil.encodePathQuery(path)); generateIfHeader(method); if (getGetContentType() != null && !getGetContentType().equals("")) method.setRequestHeader("Content-Type", getGetContentType()); method.setRequestContentLength(PutMethod.CONTENT_LENGTH_CHUNKED); method.setRequestBody(is); generateTransactionHeader(method); int statusCode = client.executeMethod(method); setStatusCode(statusCode); return (statusCode >= 200 && statusCode < 300) ? true : false; }