需要依赖的jar包有:
commons-beanutils.jar
commons-collections-3.2.2.jar
commons-lang-2.4.jar
commons-logging.jar
ezmorph-1.0.6.jar
httpclient-4.5.jar
httpcore-4.4.1.jar
json-lib-2.2.2-jdk15.jar
package com.devide.util;
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
/**
* 和服务器通讯的工具类
* @author Administrator
*
*/
public class HttpUtil {
public static final String URL = "http://192.168.168.168:8080/DivideClassServer/getData";
public static String sendPost(String url, String jsonParams,
String encoding) throws ClientProtocolException, IOException
{
int statusCode = 0;
String body = "";
String resultCode = "";
// 设置协议http和https对应的处理socket链接工厂的对象
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory> create()
.register("http", PlainConnectionSocketFactory.INSTANCE).build();
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
HttpClients.custom().setConnectionManager(connManager);
// 创建自定义的httpclient对象
CloseableHttpClient client = HttpClients.custom().setConnectionManager(connManager).build();
// 创建post方式请求对象
HttpPost httpPost = new HttpPost(url);
StringEntity uefEntity = new StringEntity(jsonParams, encoding);
uefEntity.setContentType("application/json");
// 设置参数到请求对象中
httpPost.setEntity(uefEntity);
// 执行请求操作,并拿到结果(同步阻塞)
CloseableHttpResponse response = client.execute(httpPost);
statusCode = response.getStatusLine().getStatusCode();
if (200 != statusCode)
{
resultCode = "-1";
return resultCode;
}
// 获取结果实体
HttpEntity entity = response.getEntity();
if (entity != null)
{
// 按指定编码转换结果实体为String类型
body = EntityUtils.toString(entity, encoding);
resultCode = getJson(body);
}
EntityUtils.consume(entity);
// 释放链接
response.close();
return resultCode;
}
//解析json串,获取resultCode值
private static String getJson(String body)
{
JSONArray arr = JSONArray.fromObject(body);
System.out.println("返回的数组长度:"+arr.size());
System.out.println(arr);
if(arr.size()==0) return "";
JSONObject obj = null;
for (int i = 0; i < arr.size(); i++) {
obj = arr.getJSONObject(i);
System.out.println(obj.toString());
}
String resultCode = obj.getString("students");
System.out.println("获取到服务器返回的数据:"+resultCode);
return resultCode;
}
public static void main(String[] args){
String result = HttpUtil.sendPost(URL, "", "utf-8");
System.out.println(result);
}
}