java 发送 soap 消息 返回xml格式字符串
2014-11-12 19:15
393 查看
private static final String NAMESPACE_URI = "http://tempurl.org/";
private static final String CONTENT_TYPE =
private static final String HIS_SERVICE_KEY =
private static final String HIS_SERVICE_URL =
SOAPConnectionFactory soapConnFactory = SOAPConnectionFactory.newInstance();
SOAPConnection connection = soapConnFactory.createConnection();
MessageFactory messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
SOAPMessage message = messageFactory.createMessage();
MimeHeaders hd = message.getMimeHeaders();
hd.setHeader("Content-Type", CONTENT_TYPE);
hd.addHeader("SOAPAction", NAMESPACE_URI + methodName);
SOAPPart soapPart = message.getSOAPPart();
//Create objects for the message parts
SOAPEnvelope envelope = soapPart.getEnvelope();
SOAPBody body = envelope.getBody();
//创建根节点填充Soap body。
SOAPElement bodyElement = body.addChildElement(envelope.createName(methodName, null, NAMESPACE_URI));
Collection<String> c = params.values();
Set<String> keys = params.keySet();
for (Iterator it = keys.iterator(); it.hasNext();) {
String paramKey = (String) it.next();
String paramValue = params.get(paramKey);
bodyElement.addChildElement(paramKey).addTextNode(paramValue);
}
//保存message
message.saveChanges();
logger.debug("\\nREQUEST:\\n" + message);
//发送请求获取相应
String destination = HIS_SERVICE_URL;
SOAPMessage reply = connection.call(message, destination);
logger.debug("\\nREQUEST:\\n" + message);
//Create the transformer
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
//提取响应的xml
Source sourceContent = reply.getSOAPPart().getContent();
//Set the output for the transformation
StreamResult result = new StreamResult(System.out);
transformer.transform(sourceContent, result);
logger.debug("\\nRESPONSE:\\n" + result);
String response = reply.getSOAPBody().getTextContent();
return response;
返回的response可以解析成dom对象
private static final String CONTENT_TYPE =
private static final String HIS_SERVICE_KEY =
private static final String HIS_SERVICE_URL =
SOAPConnectionFactory soapConnFactory = SOAPConnectionFactory.newInstance();
SOAPConnection connection = soapConnFactory.createConnection();
MessageFactory messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
SOAPMessage message = messageFactory.createMessage();
MimeHeaders hd = message.getMimeHeaders();
hd.setHeader("Content-Type", CONTENT_TYPE);
hd.addHeader("SOAPAction", NAMESPACE_URI + methodName);
SOAPPart soapPart = message.getSOAPPart();
//Create objects for the message parts
SOAPEnvelope envelope = soapPart.getEnvelope();
SOAPBody body = envelope.getBody();
//创建根节点填充Soap body。
SOAPElement bodyElement = body.addChildElement(envelope.createName(methodName, null, NAMESPACE_URI));
Collection<String> c = params.values();
Set<String> keys = params.keySet();
for (Iterator it = keys.iterator(); it.hasNext();) {
String paramKey = (String) it.next();
String paramValue = params.get(paramKey);
bodyElement.addChildElement(paramKey).addTextNode(paramValue);
}
//保存message
message.saveChanges();
logger.debug("\\nREQUEST:\\n" + message);
//发送请求获取相应
String destination = HIS_SERVICE_URL;
SOAPMessage reply = connection.call(message, destination);
logger.debug("\\nREQUEST:\\n" + message);
//Create the transformer
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
//提取响应的xml
Source sourceContent = reply.getSOAPPart().getContent();
//Set the output for the transformation
StreamResult result = new StreamResult(System.out);
transformer.transform(sourceContent, result);
logger.debug("\\nRESPONSE:\\n" + result);
String response = reply.getSOAPBody().getTextContent();
return response;
返回的response可以解析成dom对象
相关文章推荐
- Java解析XML格式字符串返回Document类型对象(2009-05-01 04:33:26)转载标签:javaxmldocument杂谈 分类:软件,网络
- Webservice的wsdl文件解析与Soap消息的发送、接收(不生成java客户端代码)
- java使用POST发送soap报文请求webservice返回500错误解析
- java语言搭建SSL的Socket并发送字符串消息(最简单应用实例/常见异常及解决)
- java使用POST发送soap报文请求webservice返回500错误解析
- Web Service学习笔记(webservice、soap、wsdl、jws详细分析) Webservice的wsdl文件解析与Soap消息的发送、接收(不生成java客户端代码)
- Java web 项目发送请求到 thinkphp 项目返回Json字符串
- 求助在java(java web)中发送接收soap消息。
- java 发送http请求 返回字符串 再进行解析(略)
- 使用 Web Services Enhancements 发送带有附件的 SOAP 消息
- Delphi窗体间发送消息或字符串
- c# 发送post请求到服务器,并解析得到的xml格式的字符串
- 基于Servlet发送、接收SOAP消息
- JavaMail基础知识2-JavaMail API使用之发送消息
- 用Java向IPMSG发送消息
- Java发送Http请求,解析html返回
- 使用SAAJ发送和接收SOAP消息
- 用Java向IPMSG发送消息
- 回复:String中如果有中文,如何保证length()返回的长度 在 Java 中字符串是以 Unicode 方式编码的,所以,无论中英文都算的长度为 1。根据你的描述,估计是想要取得英文长度为 1 中文长度为 2 的总长度吧。这个,理论上可以构造 An
- JAVA发送HTTP请求,返回HTTP响应内容,实例及应用