Java 使用 HttpClient 发送 get post 请求 数据

直接上代码

// 创建CookieStore实例
	CookieStore cookieStore = null;
	HttpClientContext context = null;
	String loginUrl = "http://cn.grustv.com/xxxx";
	String getTerminals = "http://cn.grustv.com/xxxx" + System.currentTimeMillis();
	String getDeviceStatus = "http://cn.grustv.com/index/terminal/getDeviceStatus?tid=";
	String getDeviceStatusEnd = "&type=hlink_status%7Cforward_info";
	@Action(value = "/xxxx", results = { @Result(type = "json", params = { "root", "xxxx" }) })
	public void getxxxx() {
		//获取设备拉流地址集合
		List<Object> rtmplist = new ArrayList<Object>();
		// 创建HttpClientBuilder
		HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
		// HttpClient
		CloseableHttpClient client = httpClientBuilder.build();
		// 直接创建client
		// CloseableHttpClient client = HttpClients.createDefault();
		HttpPost httpPost = new HttpPost(loginUrl);
		Map<String, String> parameterMap = new HashMap<String, String>();
		parameterMap.put("name", "xxxx");
		parameterMap.put("password", "xxxx");
		parameterMap.put("remeber", "xxxx");
		UrlEncodedFormEntity postEntity = null;
		try {
			postEntity = new UrlEncodedFormEntity(getParam(parameterMap),"UTF-8");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		httpPost.setEntity(postEntity);
		try {
			// 执行post请求
			HttpResponse httpResponse = client.execute(httpPost);
			// 执行get请求
			HttpGet httpGet = new HttpGet(getTerminals);
			HttpResponse httpResponse1 = client.execute(httpGet);
			//设备 ID 集合
			List<Object> idlist = new ArrayList<Object>();
			// 获取响应消息实体
			HttpEntity entity = httpResponse1.getEntity();
			// 判断响应实体是否为空
			if (entity != null) {
				String responseString = EntityUtils.toString(entity);
				JSONObject jsonInfo = JSONObject.parseObject(responseString);
				JSONArray array = JSONArray.parseArray(jsonInfo.getString("list"));
				for (int i = 0; i < array.size(); i++) {
					idlist.add(array.getJSONObject(i).get("tid"));
				}
			}
			if (!idlist.isEmpty()) {
				for (Object object : idlist) {
					HttpGet httpTerminal = new HttpGet(getDeviceStatus + object + getDeviceStatusEnd);
					HttpResponse httpResponse2 = client.execute(httpTerminal);
					// 获取响应消息实体
					HttpEntity entity2 = httpResponse2.getEntity();
					// 判断响应实体是否为空
					if (entity2 != null) {
						Map<String,Object> rtmpMap = new HashMap<String,Object>();
						String responseString = EntityUtils.toString(entity2);
						JSONObject jsonInfo = JSONObject.parseObject(responseString);
						// 判断终端工作状态 
						if ((boolean) jsonInfo.get("rel")) {
							rtmpMap.put("ID" ,(String) object);
					        JSONObject preview_url = jsonInfo.getJSONObject("preview_url");
                            rtmpMap.put("Terminal", preview_url.getString("rtmp"));
							rtmplist.add(rtmpMap);
						}
					}
				}
			}
			// cookie
			setCookieStore(httpResponse);
			// context
			setContext();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				// 关闭流并释放资源
				client.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		//System.out.println(rtmplist.size());
		//rtmplist.add("wurenji001");
		//dataWrap.setDataList(rtmplist);
	    //JSONObject json = (JSONObject) JSONObject.toJSON(rtmpMap);//将map对象转换成json类型数据
	   // result = json.toString();//给result赋值,传递给页面
		responseData.setParameter("resultList",rtmplist);
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

OOObject

你的鼓励是我创作的最大源泉

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值