HttpClient 4.0 实例--简单工具类
一、基于Maven项目前期准备
1、所需jar包,本人使用的Maven项目,其中Pom.xml内容如下所示
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.3.4</version>
</dependency>
其中4.3.4只是随意使用的一个版本(只要使用4.0以上版本即可),其中所依赖的
客户端代码:
package com.xxx.test;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
public class HttpClientText {
/**
* HTTPCLIENT 4.0 POST 另外一种访问方式
*/
@SuppressWarnings({ "deprecation", "resource" })
public static void postForm() {
try {
// 定义 httpclient链接
HttpClient httpclient = new DefaultHttpClient();
// Post 访问提交方式
HttpPost httppost = new HttpPost("URL 地址");
//HttpGet httpGet = new HttpGet("URL 地址");
// 设置请求参数信息request params信息 --- 参数集合信息
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("参数名", "参数值"));
// 将参数赋值到请求参数实体信息 ---并使用指定参数的编码格式 对参数转码 转义
httppost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
// 发起会话请求获取访问结果
HttpResponse response = httpclient.execute(httppost);
//HttpResponse response = httpclient.execute(httpGet);
// 读取请求返回结果 实体信息
HttpEntity entity = response.getEntity();
// 返回结果实体Body信息
String body = EntityUtils.toString(entity);
System.err.println(body);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* HTTPCLIENT 4.0 POST 带附件File请求
*/
@SuppressWarnings({ "deprecation", "resource" })
public static void uploadFilePost() {
try {
// 定义 httpclient链接
HttpClient httpclient = new DefaultHttpClient();
// Post 访问提交方式
HttpPost httppost = new HttpPost("URL 地址");
// 设置请求超时时间
httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 50000);
// 设置读取超时时间
httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 50000);
// 设置请求参数信息request params信息 --- 参数生成器
MultipartEntityBuilder build = MultipartEntityBuilder.create();
// ContentType 指定参数编码格式、解析方式 包含多种格式 例如: "text/html",
// Consts.ISO_8859_1 等
build.addPart("appid:参数名称", new StringBody("参数值", ContentType.DEFAULT_TEXT));
// 将文件流对象写入到请求request中
build.addBinaryBody("imageFile:参数名", new File("附件地址"));
// 将参数赋值到请求参数实体信息
httppost.setEntity(build.build());
// 发起会话请求获取访问结果
HttpResponse response = httpclient.execute(httppost);
// 读取请求返回结果 实体信息
HttpEntity entity = response.getEntity();
// 返回结果实体Body信息
String body = EntityUtils.toString(entity);
System.err.println(body);
} catch (Exception e) {
e.printStackTrace();
}
}
}
基于SPRING MVC 实现的服务端代码,如下所示(仅给出包含File参数的服务端代码):
@RequestMapping(value = "xxx/upload_file", method = RequestMethod.POST)
public String uploadFile(String appid, MultipartFile imageFile, Model model) {
......
}