Apache HttpClient 4.5 HTTP PUT Request Method Example
This tutorial demonstrates how to use Apache HttpClient 4.5 to make a Http PUT request. The HTTP PUT Request Method requests that the server accepts and stores the entity enclosed in the supplied URI. If the URI refers to an already existing resource, it is modified; if the URI does not point to an existing resource, then the server can create the resource with that URI.
Maven dependencies
We use maven to manage our dependencies and are using Apache HttpClient version 4.5. Add the following dependency to your project in order to make HTTP PUT request method.
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.memorynotfound.apache.httpclient.httmethods</groupId> <artifactId>http-put</artifactId> <version>1.0.0-SNAPSHOT</version> <url>https://memorynotfound.com</url> <name>httpclient - ${project.artifactId}</name> <dependencies> <!-- Apache HttpClient --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.2</version> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
HTTP PUT Request Method Example
In the following example we put data to the resource http://httpbin.org/put. This resources acknowledges the data and returns a JSON object which we’ll simply print to the console. Note: we are using Java 7 try-with-resources to automatically handle the closing of the ClosableHttpClient. Next we are using Java 8 lambdas for the ResponseHandler. Here we are evaluating the Http Status code, when everything is ok we return the body of the response which we parse to a String. When the status code is not what we expect, we throw a ClientProtocolException, indicating that the Http PUT request method failed. Finally, we print the response body to the console.
package com.memorynotfound.httpclient; import org.apache.http.HttpEntity; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.ResponseHandler; import org.apache.http.client.methods.HttpPut; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import java.io.IOException; /** * This example demonstrates the use of {@link HttpPut} request method. */ public class HttpPutRequestMethodExample { public static void main(String... args) throws IOException { try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpPut httpPut = new HttpPut("http://httpbin.org/put"); httpPut.setEntity(new StringEntity("Hello, World")); System.out.println("Executing request " + httpPut.getRequestLine()); // Create a custom response handler ResponseHandler<String> responseHandler = response -> { int status = response.getStatusLine().getStatusCode(); if (status >= 200 && status < 300) { HttpEntity entity = response.getEntity(); return entity != null ? EntityUtils.toString(entity) : null; } else { throw new ClientProtocolException("Unexpected response status: " + status); } }; String responseBody = httpclient.execute(httpPut, responseHandler); System.out.println("----------------------------------------"); System.out.println(responseBody); } } }
Output
Executing request PUT http://httpbin.org/put HTTP/1.1 ---------------------------------------- { "args": {}, "data": "Hello, World!", "files": {}, "form": {}, "headers": { "Accept-Encoding": "gzip,deflate", "Connection": "close", "Content-Length": "13", "Content-Type": "text/plain; charset=ISO-8859-1", "Host": "httpbin.org", "User-Agent": "Apache-HttpClient/4.5.2 (Java/1.8.0_20)" }, "json": null, "origin": "123.123.123.123", "url": "http://httpbin.org/put" }
Download
From:一号门
COMMENTS