xml 发送 util

前端之家收集整理的这篇文章主要介绍了xml 发送 util前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
package com.xiaoma.utils.cmf; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; public class SendMessage { /** * 发送xml请求方式 * * @param xml * @param encoding * @param urlAddress * @return */ public String transferData(String xml,String encoding,String urlAddress) { HttpURLConnection conn = null; String result = ""; try { URL url = new URL(urlAddress); conn = (HttpURLConnection) url.openConnection(); conn.setRequestProperty("Content-type","text/xml"); conn.setConnectTimeout(200000); conn.setReadTimeout(200000); conn.setDoInput(true); conn.setDoOutput(true); conn.setRequestMethod("POST"); // 发送请求 OutputStream os = conn.getOutputStream(); os.write(xml.getBytes(encoding)); os.flush(); os.close(); // 读取响应 BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(),encoding)); String str; while ((str = reader.readLine()) != null) { result += str; result += "\n"; } result = result.trim(); reader.close(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (conn != null) { conn.disconnect(); } } return result; } } 原文链接:https://www.f2er.com/xml/297585.html

猜你在找的XML相关文章