目录
1.StringEntity
首先说StringEntity
StringEntity extends AbstractHttpEntity implements Cloneable
继承于AbstractHttpEntity类,这个类做参数的时候设置比较灵活,通过类的构造方法可以看到有两个参数,一个是具体的参数值(string串),另一个是ContentType,通过重写的构造方法看到,当不指定ContentType时默认的ContentType是text/plain,具体看下图构造方法:
默认通过一个参数构造方法创建对象,ContentType得值是 ContentType.DEFAULT_TEXT,找到来源,
DEFAULT_TEXT的值是 create(“text/plain”, Consts.ISO_8859_1)设定的
HttpPost post=new HttpPost(“https://api.weixin.qq.com/cgi-bin/template/api_set_industry?access_token=ACCESS_TOKEN”);
JSONObject param=new JSONObject();
param.put(“key”,“value”);
StringEntity requestentity=new StringEntity(param.toString());
requestentity.setContentType(ContentType.APPLICATION_JSON.toString());
post.setEntity(requestentity);
所以当要通过post发送json数据时就要额外设置Contentype为application/json或在初始化时传入一个Contentype类型。其他类型也一样可以自由设置,要注意的就是默认值,以免忘记设定而造成请求出错。
2.UrlEncodeFormEntity
UrlEncodeFormEntity继承字上面的StringEntity ,可以认为指定类型的entity,说到指定类型,UrlEncodeFormEntity默认指定了他的ContentType就是application/x-www-form-urlencoded,所以使用这个类做参数,会将参数以key1=value1&key2=value2的键值对形式发出。类似于传统的application/x-www-form-urlencoded表单上传。
对于ContentType指定的值:
从构造方法看到在初始化时就指定了ContentType,也就决定了参数的形式,还要注意到参数是以UrlEncodeFormEntity放到HttpPost对象里的,但是UrlEncodeFormEntity创建时必须是NameValuePair类型的对象或对象的集合作为他的构造参数,不能是String类型,上面的构造方法里面可以看到参数类型是Iterable<? extends NameValuePair> parameters,这是一个死的限制,也就是说要用UrlEncodeFormEntity作为参数传递,那数据格式组装就必须要用NameValuePair类型:
List<NameValuePair> params=new ArrayList<>();
params.add(new BasicNameValuePair("key1","value1"));
params.add(new BasicNameValuePair("key2","value2"));
params.add(new BasicNameValuePair("key3","value3"));
UrlEncodedFormEntity entity=new UrlEncodedFormEntity(params,"UTF-8");
post.setEntity(entity);
如上面示例代码,只能是这种类型构建参数
综上所述,StringEntity可以用来灵活设定参数格式形式,而UrlEncodeFormEntity则适合于传统表单格式的参数形式,至于到底用什么形式,也要看请求目标服务的数据接收形式。
例子:
public static String send(String url, JSONObject jsonObject, String encoding) throws Exception, IOException {
String body = "";
//创建httpclient对象
CloseableHttpClient client = HttpClients.createDefault();
//创建post方式请求对象
HttpPost httpPost = new HttpPost(url);
//装填参数
StringEntity s = new StringEntity(""}", "utf-8");
s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
"application/json"));
//设置参数到请求对象中
httpPost.setEntity(s);
System.out.println("请求地址:" + url);
if (header != null) {
Set<Map.Entry <String, String>> entrySet = header.entrySet();
for (Map.Entry <String, String> entry : entrySet) {
// 设置到请求头到HttpRequestBase对象中
httpPost.setHeader(entry.getKey(), entry.getValue());
}
}
//执行请求操作,并拿到结果(同步阻塞)
CloseableHttpResponse response = client.execute(httpPost);
//获取结果实体
HttpEntity entity = response.getEntity();
if (entity != null) {
//按指定编码转换结果实体为String类型
body = EntityUtils.toString(entity, encoding);
}
EntityUtils.consume(entity);
//释放链接
response.close();
return body;
}