Test.java 2.22 KB
//package com.bsth;
//
//import java.io.ByteArrayOutputStream;
//import java.io.IOException;
//import java.io.InputStream;
//import java.io.OutputStream;
//import java.net.HttpURLConnection;
//import java.net.URL;
//import java.util.HashMap;
//import java.util.Map;
//
//import org.apache.commons.io.IOUtils;
//
//import com.fasterxml.jackson.databind.ObjectMapper;
//
//public class Test {
//	
//	public static void main(String[] args) {
//		Map<String, Object> map = new HashMap<>();
//		map.put("timestamp", 1469764939000L);
//		map.put("operCode", 128);
//		map.put("deviceId", "66ML0007");
//		
//		Map<String , Object> dataMap = new HashMap<>();
//		dataMap.put("requestCode", 161);
//		dataMap.put("lineId", 1025);
//		dataMap.put("operCode2", 38);
//		
//		map.put("data", dataMap);
//		request(map, "http://192.168.168.171:9088/control/upstream");
//		
//	}
//	
//	private static Map<String, Object> request(Map<String, Object> map, String url) {
//		InputStream in = null;
//		OutputStream out = null;
//		HttpURLConnection con = null;
//		try {
//			con = (HttpURLConnection)new URL(url).openConnection();
//			con.setRequestMethod("POST");
//			con.setRequestProperty("keep-alive", "true");
//			con.setRequestProperty("accept", "*/*");
//			con.setDoInput(true);
//			con.setDoOutput(true);
//			con.setReadTimeout(2500);
//			con.setConnectTimeout(2500);
//			out = con.getOutputStream();
//			out.write("json=".getBytes());
//			out.write(new ObjectMapper().writeValueAsBytes(map));
//			out.flush();
//			System.out.println("con.getResponseCode(): " + con.getResponseCode());
//			if (con.getResponseCode() == 200) {
//				in = con.getInputStream();
//				ByteArrayOutputStream bout = new ByteArrayOutputStream();
//				IOUtils.copy(in, bout); bout.close();
//				System.out.println("Message:" + new String(bout.toByteArray()));
//				return new ObjectMapper().readValue(bout.toByteArray(), Map.class);
//			}
//		} catch (IOException e) {
//			e.printStackTrace();
//		} finally {
//			con.disconnect();
//			try {
//				if (in != null) in.close();
//				if (out != null) out.close();
//			} catch (IOException e) {
//				e.printStackTrace();
//			}
//		}
//		return null;
//	}
//}