首页 > 代码库 > 使用Apache HttpClient 4.x发送Json数据
使用Apache HttpClient 4.x发送Json数据
Apache HttpClient是Apache提供的一个开源组件,使用HttpClient可以很方便地进行Http请求的调用。自4.1版本开始,HttpClient的API发生了较大的改变,很多方法的调用方式已经和3.x版本不同。本文使用的是当前最新的4.5.3版本。
首先在pom文件中引入httpcomponents依赖包:
1 <dependency> 2 <groupId>org.apache.httpcomponents</groupId> 3 <artifactId>httpclient</artifactId> 4 <version>4.5.3</version> 5 </dependency>
本文展示的是POST请求。
1 import java.io.IOException; 2 3 import org.apache.commons.lang3.ObjectUtils; 4 import org.apache.commons.lang3.StringUtils; 5 import org.apache.http.Consts; 6 import org.apache.http.HttpStatus; 7 import org.apache.http.ParseException; 8 import org.apache.http.client.config.RequestConfig; 9 import org.apache.http.client.methods.CloseableHttpResponse; 10 import org.apache.http.client.methods.HttpPost; 11 import org.apache.http.entity.ContentType; 12 import org.apache.http.entity.StringEntity; 13 import org.apache.http.impl.client.CloseableHttpClient; 14 import org.apache.http.impl.client.HttpClients; 15 import org.apache.http.util.EntityUtils; 16 17 /** 18 * @author 19 * 20 * @date 2017年5月18日 上午9:17:30 21 * 22 * @Description 23 */ 24 public class HttpPostUtils { 25 /** 26 * 27 * @param uri 28 * the request address 29 * @param json 30 * the request data that must be a JSON string 31 * @param connectTimeout 32 * the timeout in milliseconds until a connection is established 33 * @param connectionRequestTimeout 34 * the timeout in milliseconds used when requesting a connection 35 * from the connection manager 36 * @param socketTimeout 37 * the socket timeout in milliseconds, which is the timeout for 38 * waiting for data or, put differently, a maximum period 39 * inactivity between two consecutive data packets 40 * @return null when method parameter is null, "", " " 41 * @throws IOException 42 * if HTTP connection can not opened or closed successfully 43 * @throws ParseException 44 * if response data can not be parsed successfully 45 */ 46 public String postJson(String uri, String json, int connectTimeout, int connectionRequestTimeout, int socketTimeout) 47 throws IOException, ParseException { 48 if (StringUtils.isAnyBlank(uri, json)) { 49 return null; 50 } 51 52 CloseableHttpClient client = HttpClients.createDefault(); 53 HttpPost post = new HttpPost(uri); 54 // Create request data 55 StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON); 56 // Set request body 57 post.setEntity(entity); 58 59 RequestConfig config = RequestConfig.custom().setConnectTimeout(connectTimeout) 60 .setConnectionRequestTimeout(connectionRequestTimeout).setSocketTimeout(socketTimeout).build(); 61 post.setConfig(config); 62 // Response content 63 String responseContent = null; 64 CloseableHttpResponse response = null; 65 try { 66 response = client.execute(post); 67 if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { 68 responseContent = EntityUtils.toString(response.getEntity(), Consts.UTF_8.name()); 69 } 70 } finally { 71 if (ObjectUtils.anyNotNull(response)) { 72 response.close(); 73 } 74 if (ObjectUtils.anyNotNull(client)) { 75 client.close(); 76 } 77 } 78 return responseContent; 79 } 80 }
使用Apache HttpClient 4.x发送Json数据
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。