Java send and receive messages

A message is a unit of data exchanged and transmitted in the network, that is, a block of data to be sent by a station at one time. The message contains the complete data information to be sent, its length is very inconsistent, and its length is unlimited and variable.

Personal understanding: the string is written into the byte array stream from the client to the server, but the string is in XML format, and then on the server, the byte array is used to obtain the string, and then the string is obtained The document object (because the string is in xml format), and then parse to get the data.

Send message

First create a method to generate messages, add xml data

/**
	 * @desc 生成xml报文且转换为字符串
	 * @return
	 */
	public String xmlToString() {
    
    
		StringBuffer sendStr = new StringBuffer();
		// 以下为添加的xml信息
		sendStr.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");

		sendStr.append("<request_data>");
		sendStr.append("<request_token>BCDBCD</request_token>");
		sendStr.append("<request_cardNum>62284801912705</request_cardNum>");
		sendStr.append("<request_name>张飞</request_name>");
		sendStr.append("<request_pass>123123</request_pass>");
		sendStr.append("<request_time>"+new Dates().CurrentYMDHSMTime()+"</request_time>");


		sendStr.append("<monery_count>200.00</monery_count>");
		sendStr.append("<shop_name>吉野家</shop_name>");
		sendStr.append("<shop_id>JYJ</shop_id>");
		sendStr.append("<sale_no>"+UUID.randomUUID().toString()+"</sale_no>");
		sendStr.append("</request_data>");
		// 创建字符串用于返回
		String str = sendStr.toString();
		return str;
	}

Convert message xml to stream for writing

/**
	 * @desc 将xml转为流写入
	 * @param servletConnection
	 * @throws IOException 
	 */
	public void xmlWriteStream(HttpURLConnection servletConnection) throws IOException {
    
    
		// 创建流,写入xml数据
		OutputStream ouput = servletConnection.getOutputStream();
		// 调用方法获取xml字符串
		String str = xmlToString();
		System.out.println(str);
		// 将xml字符串转为btye数组写入流
		ouput.write(str.getBytes("UTF-8"));
		ouput.flush();// 刷新流
		ouput.close();// 关闭流
	}

Create a connection between the client and the server and set some attributes of the sent message

/**
	 * @desc 创建客户端与服务端的连接并且设置发送报文的一些属性
	 * @return
	 * @throws IOException 
	 */
	public HttpURLConnection cerateServletUrl() throws IOException {
    
    
		// 服务端地址
		URL uploadServlet = new URL("http://localhost:1023/BaoWenServer/xmlServlet.do");
		// 创建连接
		HttpURLConnection servletConnection = (HttpURLConnection) uploadServlet.openConnection();
		// 设置连接参数
		servletConnection.setRequestMethod("POST"); // 请求类型为post
		servletConnection.setDoOutput(true); // 是否可读
		servletConnection.setDoInput(true); // 是否可写
		servletConnection.setAllowUserInteraction(true); // 是否可交互
		return servletConnection;
	}

Get the results returned by the server

/**
	 * @desc 获取服务端反馈回来的结果
	 * @param servletConnection
	 * @throws IOException
	 */
	public void getResponseResult(HttpURLConnection servletConnection)
			throws IOException {
    
    
	
		System.out.println("===============**服务端的返回值**==================");
		// 获取返回的数据
		InputStream inputStream = servletConnection.getInputStream();//获取反馈回来的流
		//创建一个缓冲读取的reader对象
		BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
		//创建一个能够承接返回来值得sb
		StringBuffer sb = new StringBuffer();
        //创建一个能够临时存储读取一行信息的变量
		String strMessage = "";
		//开始循环读取返回的流信息
		while ((strMessage = reader.readLine()) != null) {
    
    
			sb.append(strMessage);//将返回的流的信息逐行的压如到sb中
		}

		System.out.println("接收返回值:" + sb);
	}

The last call method

/**
	 * @throws IOException 
	 * @desc 用于调用方法发送报文的方法
	 */
	public void sendMessage() throws IOException {
    
    
		try {
    
    
			System.out.println("=================开始发送报文=================");
			// 建立连接
			HttpURLConnection servletConnection = cerateServletUrl();
			// 写入数据
			xmlWriteStream(servletConnection);
			// 获取返回数据
			getResponseResult(servletConnection);
		} catch (java.net.ConnectException e) {
    
    
			System.out.println("客户端与服务端连接异常,请再次尝试");
		}
	}

Receive message

The server uses the web project to build
and sets the code set in the service

		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");

Get the message sent by the client

//----------------通过request.getInputStream()获取输入的流----------------------
	// 指定每次8kb
	final int BUFFER_SIZE = 8 * 1024;
	byte[] buffer = new byte[BUFFER_SIZE];
	// 获取输出流,与客户端的输出流相对应
	ServletInputStream sis = request.getInputStream();
//	System.out.println("sis:"+sis);
			
	int length = 0;
	// 创建字节输出流
	ByteArrayOutputStream baos = new ByteArrayOutputStream();

	do {
    
    
		length = sis.read(buffer);
		if (length > 0) {
    
    
			// 写入至字节输出流
			baos.write(buffer, 0, length);
		}
	} while (length * 2 == BUFFER_SIZE);
	// 把字节输出流转为字符串,得到客户端发送的数据,即为报文
	String bodyData = new String(baos.toByteArray());

This bodyDatais the message data sent by the client:

<?xml version="1.0" encoding="utf-8"?>
<request_data>
	<request_token>BCDBCD</request_token>
	<request_cardNum>62284801912705</request_cardNum>
	<request_name>张飞</request_name>
	<request_pass>123123</request_pass>
	<request_time>2021年01月25日 14:51:52</request_time>
	<monery_count>200.00</monery_count>
	<shop_name>吉野家</shop_name>
	<shop_id>JYJ</shop_id>
	<sale_no>fc4c66dc-b54b-4052-89c1-902be7569f18</sale_no>
</request_data>

Read the xml to analyze the data : Zhang Fei spent 200.00 yuan at Yoshinoya whose merchant id is JYJ at 14:51:52 on January 25, 2021. The serial number of this consumption is fc4c66dc-b54b-4052-89c1-902be7569f18 , The card number used is 62284801912705, the card password is 123123 (without encryption), and the current identification code is BCDBCD

Parse the message

The message data sent by the client is of Xml type, so it can be parsed directly with Xml data analysis method

	// 获取报文之后,开始解析报文,即为解析Xml
	//1.初始化jdk中的用来解析xml的dom工厂
	DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
	//2:获得具体的dom解析器
	DocumentBuilder db = dbf.newDocumentBuilder();
	//3: 解析一个xml文档,获得Document对象(根结点)
	InputSource is = new InputSource(new StringReader(bodyData));
		
	Document document = null;
	try{
    
    
		document = db.parse(is);//将流转换成document对象
	}catch(Exception e){
    
    			
		return ;
	}

After obtaining the document object, start parsing

	//通过抓取根节点进而获取子节点
	NodeList list = document.getElementsByTagName("request_data");
	Map<String, Object> map = new HashMap<String, Object>();//将抓取之后获得到的值存在map中
	XmlServiceImpl service = new XmlServiceImpl();
	for (int i = 0; i < list.getLength(); i++) {
    
    
		Element element = (Element) list.item(i);//通过item(i)获取根节点下的每一个子节点
		//1.识别码
		String request_token = element.getElementsByTagName("request_token").item(0).getFirstChild().getNodeValue();
		map.put("request_token", request_token);

		//2.卡号
		String request_cardNum = element.getElementsByTagName("request_cardNum").item(0).getFirstChild().getNodeValue();
		map.put("request_cardNum", request_cardNum);

		//3.持卡人姓名
		String request_name = element.getElementsByTagName("request_name").item(0).getFirstChild().getNodeValue();
		map.put("request_name", request_name);

		//4.该卡的密码
		String request_pass = element.getElementsByTagName("request_pass").item(0).getFirstChild().getNodeValue();
		map.put("request_pass", request_pass);

		//5.本次消费请求的时间
		String request_time = element.getElementsByTagName("request_time").item(0).getFirstChild().getNodeValue();
		map.put("request_time", request_time);
				
		//6.本次消费的金额
		String monery_count = element.getElementsByTagName("monery_count").item(0).getFirstChild().getNodeValue();
		map.put("monery_count", monery_count);
				
		  
		//7.本次消费到的商家的名字
		String shop_name = element.getElementsByTagName("shop_name").item(0).getFirstChild().getNodeValue();
		map.put("shop_name", shop_name);
									
				
		//8.本次消费到的商家的id
		String shop_id = element.getElementsByTagName("shop_id").item(0).getFirstChild().getNodeValue();
		map.put("shop_id", shop_id);
								
		//9.本次消费到的流水单号
		String sale_no = element.getElementsByTagName("sale_no").item(0).getFirstChild().getNodeValue();
		map.put("sale_no", sale_no);		  			   	
	}

Output map object at this time, view data

{request_name=张飞, shop_id=JYJ, request_time=2021年01月25日 14:51:52, request_token=BCDBCD, monery_count=200.00, sale_no=fc4c66dc-b54b-4052-89c1-902be7569f18, request_cardNum=62284801912705, shop_name=吉野家, request_pass=123123}

Return message

After everything is correct, return the message

	// 要返回的报文
	StringBuffer resultBuffer = new StringBuffer();
	resultBuffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
	resultBuffer.append("<request_data>");
	resultBuffer.append("<request_name>"+request_name+"</request_name>");
	resultBuffer.append("<request_cardNum>"+request_cardNum+"</request_cardNum>");
	resultBuffer.append("<request_time>"+new Dates().CurrentYMDHSMTime()+"</request_time>");
	resultBuffer.append("<shop_name>"+shop_name+"</shop_name>");
	resultBuffer.append("<sale_no>"+sale_no+"</sale_no>");
	resultBuffer.append("<request_token>成功了</request_token>");
	resultBuffer.append("</request_data>");
	 // 设置发送报文的格式
	response.setContentType("text/xml");
	response.setCharacterEncoding("UTF-8");
						  
	PrintWriter out = response.getWriter();
	out.println(resultBuffer.toString()); 
	out.flush();
	out.close();

Learning exchange q group: 470464545, everyone study and progress together, CSDN blog: lolly1023

Guess you like

Origin blog.csdn.net/lolly1023/article/details/113112403