HttpClient4.5 post

	/**
	 * POST请求
	 * @param url 请求地址
	 * @param data 请求数据
	 * @return 响应数据
	 * @throws Exception
	 */
	public String post(String url, String data) throws Exception{
		HttpClient httpClient = null;
		HttpPost httpPost = null;
		HttpResponse response = null;
		String result = "";
		try {
			httpClient = HttpClients.createDefault();
			httpPost = new HttpPost(url);
			RequestConfig requestConfig = RequestConfig.custom()
					.setConnectionRequestTimeout(connectionTimeout)
					.setConnectTimeout(connectionTimeout)
					.setSocketTimeout(connectionTimeout)
					.build();
			httpPost.setConfig(requestConfig);
			
			StringEntity dataEntity = new StringEntity(data, Charset.forName("UTF-8"));
			httpPost.setEntity(dataEntity);
			response = httpClient.execute(httpPost);
			int retCode = response.getStatusLine().getStatusCode();
			if (200 == retCode){
				HttpEntity entity = response.getEntity();
				InputStream instream = entity.getContent();
				// 不超过2048M的内容可以转
				int total = (int) entity.getContentLength();
				byte[] bytes = new byte[total];
				int readCount = 0;
				int countTemp = 0;
				if ((countTemp = instream.read(bytes, readCount, total)) != -1) {
					readCount += countTemp;
				}
				result = new String(bytes, "UTF-8");
			} else {
				throw new Exception("请求失败,状态码为[" + retCode + "]");
			}
		} catch (Exception e) {
			throw e;
		} finally {
			if (httpPost != null) {
				httpPost.releaseConnection();
			}
		}
		return result;
	}

maven文件

	<groupId>demoapi</groupId>
	<artifactId>demoapi</artifactId>
	<version>1.0</version>
	<packaging>jar</packaging>

	<name>demoapi</name>
	<url>http://maven.apache.org</url>

	<properties>
		<java.version>1.8</java.version>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<!-- 基于httpcore的一套客户端 -->
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpclient</artifactId>
			<version>4.5.2</version>
		</dependency>
		<!-- http协议实现包 -->
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpcore</artifactId>
			<version>4.4.5</version>
		</dependency>
		<!-- 包含一些通用的编码解码算法。包括一些语音编码器,Hex,Base64 -->
		<dependency>
			<groupId>commons-codec</groupId>
			<artifactId>commons-codec</artifactId>
			<version>1.10</version>
		</dependency>
		<!-- 通用日志接口 -->
		<dependency>
			<groupId>commons-logging</groupId>
			<artifactId>commons-logging</artifactId>
			<version>1.2</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.7.21</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.7.21</version>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.17</version>
		</dependency>
		<!-- 用于测试 -->
		<dependency>
			<groupId>org.mockito</groupId>
			<artifactId>mockito-core</artifactId>
			<version>2.3.0</version>
		</dependency>
	</dependencies>
	<build>
		<finalName>demoapi</finalName>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.6.0</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>
		</plugins>
	</build>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值