本教程演示如何使用Apache HttpClient 4.5
發(fā)出Http PUT請(qǐng)求。 HTTP PUT請(qǐng)求方法請(qǐng)求服務(wù)器接受并存儲(chǔ)提供的URI中包含的實(shí)體。 如果該URI引用已經(jīng)存在的資源,則該資源被修改; 如果URI不指向現(xiàn)有資源,則服務(wù)器可以使用該URI創(chuàng)建資源。
我們使用maven來管理依賴關(guān)系,并使用Apache HttpClient 4.5
版本。 將以下依賴項(xiàng)添加到您的項(xiàng)目中,以便創(chuàng)建HTTP PUT請(qǐng)求方法。
pom.xml 文件的內(nèi)容如下 -
<?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.yiibai.httpclient.httmethods</groupId>
<artifactId>http-get</artifactId>
<version>1.0.0-SNAPSHOT</version>
<url>https://memorynotfound.com</url>
<name>httpclient - ${project.artifactId}</name>
<dependencies>
<!-- Apache Commons IO -->
<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>
在以下示例中,我們將數(shù)據(jù)發(fā)布到資源URL:http://httpbin.org/put
。 該資源確認(rèn)數(shù)據(jù)并返回一個(gè)JSON對(duì)象,我們只需將其打印到控制臺(tái)。 注意:使用Java7
的try-with-resources
來自動(dòng)處理關(guān)閉ClosableHttpClient
。 接下來使用Java 8
的lambda
作為ResponseHandler
。 在這里,根據(jù)Http狀態(tài)代碼判斷返回狀態(tài),當(dāng)一切正常時(shí),我們會(huì)將解析的響應(yīng)正文返回給String。 當(dāng)狀態(tài)碼不是所期望的時(shí)候,將拋出一個(gè)ClientProtocolException
,表明Http PUT請(qǐng)求方法失敗。 最后,我們將響應(yīng)主體打印到控制臺(tái)。
文件:HttpPutRequestMethodExample.java -
package com.yiibai.httpdemo;
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);
}
}
}
執(zhí)行上面示例代碼,得到以下結(jié)果 -
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": "12",
"Content-Type": "text/plain; charset=ISO-8859-1",
"Host": "httpbin.org",
"User-Agent": "Apache-HttpClient/4.5.5 (Java/1.8.0_65)"
},
"json": null,
"origin": "112.67.166.104",
"url": "http://httpbin.org/put"
}