/**
* sendUrl:接口地址;soapXML:要发送的xml数据(入参,命名空间)
* */
public static String send(String sendUrl, String soapXML) throws IOException {
// 创建服务地址
URL url = new URL(sendUrl);
// 打开一个通向服务地址的连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// 发送方式设置:POST必须大写
connection.setRequestMethod("POST");
// 设置数据格式:content-type,为xml格式,字符编码是UTF-8
connection.setRequestProperty("content-type", "text/xml;charset=UTF-8");
// 设置输入输出,读写权限,
connection.setDoInput(true);
connection.setDoOutput(true);
// 将信息以流的方式发送出去
OutputStream os = connection.getOutputStream();
os.write(soapXML.getBytes("UTF-8"));
// 接收服务端响应
int responseCode = connection.getResponseCode();
StringBuilder sb = new StringBuilder();
// 服务端响应成功
if (200 == responseCode) {
// 获取当前连接请求返回的数据流
InputStream is = connection.getInputStream();
InputStreamReader isr = new InputStreamReader(is, "UTF-8");
BufferedReader br = new BufferedReader(isr);
String temp = null;
while (null != (temp = br.readLine())) {
sb.append(temp);
}
is.close();
isr.close();
br.close();
}
return sb.toString();
}