java客户端程序和服务器http通讯

需要依赖的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

可以点击这里下载这几个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);
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值