Java编写HTTP请求发送XML报文数据



测试背景:

现在有系统A和系统B,系统B使用HTTP请求向系统A发送报文数据,然后A系统接收到对应HTTP请求,并且对该请求中数据进行解析处理,最终返回与处理逻辑相对应的XML报文。

  • 这里的系统A可以抽象理解为服务接收方,系统B可以抽象理解为服务请求方
  • 这里的xml,可以抽象一下, 并不一定需要是真正的xml报文数据,Json同理。
  • 为方便后期改写,故demo中的系统A使用SpringBoot进行构建,系统B使用测试类进行构建。




1、编写系统A代码(服务接收方)

注意:

  1. @Slf4j注解:使用了lombok插件和引入对应的maven依赖。你也可以直接使用System.out.println进行打印输出
  2. requestData:xml格式的报文数据,xml测试片段
  3. 代码逻辑分为两部分:处理接收到的数据;回写处理完的数据
@RequestMapping("/local")
@Controller
@Slf4j
public class LocalController {

	@RequestMapping("/local")
	public void testOtherClient(HttpServletRequest request, HttpServletResponse response) throws IOException {

		String unifiedCharset = "UTF-8";

		// 获取服务端接收的报文信息:
		// 1.获取输入流
		// 2.封装为一个BufferedReader
		// 3.读取数据写入Java缓存
		try (BufferedReader reader = new BufferedReader(
				new InputStreamReader(request.getInputStream()))) {
			String lines;
			StringBuffer sbf = new StringBuffer();
			while ((lines = reader.readLine()) != null) {
				lines = new String(lines.getBytes(), unifiedCharset);
				sbf.append(lines);
			}
			log.info("接收到的数据为 {}", sbf.toString());



			// 设置服务端的响应报文数据
			String resp = "<body>xml报文回复成功</body>";
			ServletOutputStream outputStream = response.getOutputStream();

			log.info("返回给客户端的报文为:" + resp);
			outputStream.write(resp.getBytes(unifiedCharset));
		} catch (RuntimeException e) {
			log.error(e.getMessage(), e);
		}
	}
}




2、编写系统B代码(服务请求方)

注意:

  1. HttpURLConnection:这是Java自带的用于构建http请求的基础类,你也可以使用类似于HttpClient这样的工具类进行HTTP请求构建
@Slf4j
public class ClientHttp {


	public static void main(String[] args) {
		sendHttp();
	}

	public static void sendHttp() {
		log.info("第一部分:处理发送的数据========================================");

		String requestMethod = "POST";
		String requestContentType = "application/xml";
		String requestUrl = "http://127.0.0.1:8080/local/local";
		String requestData = "9999999999<head>这是一条xml测试报文</head>";
		String requestCharset = "UTF-8";

		log.info("发送给 {}的请求报文为 {}", requestUrl, requestData);
		try {
			URL url = new URL(requestUrl);
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection.setDoInput(true);
			connection.setDoOutput(true);
			connection.setRequestMethod(requestMethod);
			connection.setUseCaches(false);
			connection.setInstanceFollowRedirects(true);
			connection.setRequestProperty("Content-Type", requestContentType);
			connection.connect();

			// 将发送的数据进行写入
			try (OutputStream os = connection.getOutputStream()) {
				os.write(requestData.getBytes(requestCharset));
			}

			log.info("第二部分:处理返回的数据========================================");
			// 获取服务端处理后的返回报文:
			// 1.获取输入流
			// 2.封装为一个BufferedReader
			// 3.读取数据写入Java缓存
			try (BufferedReader reader = new BufferedReader(
					new InputStreamReader(connection.getInputStream()))) {
				String lines;
				StringBuffer sbf = new StringBuffer();
				while ((lines = reader.readLine()) != null) {
					lines = new String(lines.getBytes(), requestCharset);
					sbf.append(lines);
				}
				log.info("接收到服务端返回的数据为:{}", sbf.toString());
			}
			connection.disconnect();

		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
	}
}




3、测试结果