求解requestEnvelope和requestContext怎么生成

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Vector;
import javax.mail.internet.MimeBodyPart;
import javax.xml.parsers.DocumentBuilder;
import org.apache.soap.Constants;
import org.apache.soap.SOAPException;
import org.apache.soap.messaging.Message;
import org.apache.soap.transport.SOAPTransport;
import org.apache.soap.util.xml.XMLParserUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
public class MimeSoap {

	@SuppressWarnings("unused")
	private static final String DEFAULT_HOST_URL = "http://localhost:8082/TalkWebSoap/mimeservlet.do";
	@SuppressWarnings("unused")
	private static final String DEFAULT_DATA_FILENAME = "src/hello.txt";
	@SuppressWarnings("unused")
	private static final String DEFAULT_DATA_FILENAME1 = "src/PO.xml";
	private static final String URI = "urn:oreilly-jaws-samples";
	private String m_hostURL;
	private String m_attachment;
	private String m_dataFileName;
	@SuppressWarnings("unused")
	private Node tempNode;

	public MimeSoap(String hostURL, String dataFileName, String dataFileName1) throws Exception {
		m_hostURL = hostURL;
		m_attachment = dataFileName;
		m_dataFileName = dataFileName1;
		System.out.println();
		System.out.println("_________________________________________________________");
		System.out.println("Starting GenericHTTPSoapClient:");
		System.out.println("    host url        = " + m_hostURL);
		System.out.println("    attachment      = " + m_attachment);
		System.out.println("    data file       = " + m_dataFileName);
		System.out
				.println("___________________________________________________________");
		System.out.println();
	}

	// 实际的传送工作是由sendSOAPMessage()方法完成的
	public void sendSOAPMessage() {
		Message msg = new Message();
		// 添加附件
		if (m_attachment != null) {
			BufferedReader attachmentReader;
			try {
				attachmentReader = new BufferedReader(new FileReader(m_attachment));
				StringBuffer buffer = new StringBuffer();
				for (String line = attachmentReader.readLine(); line != null; line = attachmentReader.readLine()) {
					buffer.append(line);
				}
				MimeBodyPart attachment = new MimeBodyPart();
				attachment.setText(buffer.toString());
				attachment.setHeader("Content-ID", "the-attachment"); 
				msg.addBodyPart(attachment); 
				// 首先读取XML文档,将其解析成DOM树。
	            FileReader fr = new FileReader (m_dataFileName);
	         // 通过调用Apache getXMlDocBuilder()方法得到一个解析器,它返回一个DocumentBuilder对象。
	            DocumentBuilder xdb = XMLParserUtils.getXMLDocBuilder(); 
	 
	            // 通过解析器解析文档,得到一个Document对象。
	            Document doc = xdb.parse (new InputSource (fr)); 
	            if (doc == null) { 
	                throw new SOAPException(Constants.FAULT_CODE_CLIENT, "parsing error"); 
	            } 
	            // create a vector for collecting the header elements
	            Vector<Element> headerElements = new Vector<Element>(); 
	 
	            // Create a header element in a namespace
	            org.w3c.dom.Element headerElement =  doc.createElementNS(URI,"jaws:MessageHeader"); 
	 
	            headerElement.setAttributeNS(URI,"SOAP-ENV:mustUnderstand","1"); 
	 
	            // Create subnodes within the MessageHeader
	            org.w3c.dom.Element ele = doc.createElement("From"); 
	            org.w3c.dom.Text textNode = doc.createTextNode("Me"); 
	            tempNode = ele.appendChild(textNode);
				tempNode = headerElement.appendChild(ele);
	            ele = doc.createElement("To"); 
	            textNode = doc.createTextNode("You"); 
	            tempNode = ele.appendChild(textNode); 
	            tempNode = headerElement.appendChild(ele); 
	 
	            ele = doc.createElement("MessageId"); 
	            textNode = doc.createTextNode("9999"); 
	            tempNode = ele.appendChild(textNode); 
	            tempNode = headerElement.appendChild(ele); 
	 
	            headerElements.add(headerElement); 
	 
	            // create a vector for collecting the body elements
	            Vector<Element> bodyElements = new Vector<Element>(); 
	            // 获取顶层DOM元素,放到向量中。顶层节点的下层节点元素的创建和添加工作由DOM解析器负责。
	            bodyElements.add(doc.getDocumentElement ()); 
	 
	            // Create the SOAP envelope
	            org.apache.soap.Envelope envelope = new org.apache.soap.Envelope(); 
	 
	            // Add the SOAP header element to the envelope
	            org.apache.soap.Header header = new org.apache.soap.Header(); 
	            header.setHeaderEntries(headerElements); 
	            envelope.setHeader(header); 
	           
	            // Create the SOAP body element
	            org.apache.soap.Body body = new org.apache.soap.Body(); 
	            body.setBodyEntries(bodyElements); 
	 
	            // Add the SOAP body element to the envelope
	            envelope.setBody(body); 
			    msg.send (new java.net.URL(m_hostURL), URI, envelope);				
                System.out.println("Sent SOAP Message with Apache HTTP SOAP Client."); 
                // 从传输中接受响应并将其打印到屏幕上
                System.out.println("Waiting for response...."); 
                SOAPTransport st = msg.getSOAPTransport (); 
                BufferedReader br = st.receive (); 
                String line = br.readLine(); 
                if(line == null) 
                { 
                    System.out.println("HTTP POST was successful. \n"); 
                } 
                else 
                { 
                   while (line != null) 
                   {    
                System.out.println (line); 
                        line = br.readLine(); 
                   } 
                } 
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	  public static void main(String args[]) { 
	    	try {
	    		MimeSoap client = new MimeSoap(DEFAULT_HOST_URL,DEFAULT_DATA_FILENAME,DEFAULT_DATA_FILENAME1);
			    client.sendSOAPMessage();
	    	} catch (Exception e) {
				e.printStackTrace();
			}
	    } 
}

 以上为客户端代码加入po.xml 和附件.txt

下面为服务端代码 可是服务端中requestEnvelope和requestContext如何获得了本人求解~!

//处理SOAP体 
org.apache.soap.Body body = requestEnvelope.getBody(); 
    java.util.Vector bodyEntries = body.getBodyEntries(); 
    
    writer.write("\nBody====>\n"); 
    for (java.util.Enumeration e = bodyEntries.elements(); e.hasMoreElements();) 
    { 
        org.w3c.dom.Element el = (org.w3c.dom.Element)e.nextElement(); 
        org.apache.soap.util.xml.DOM2Writer.serializeAsXML((org.w3c.dom.Node)el, writer); 
org.w3c.dom.Element attachmentEl = (org.w3c.dom.Element)el.getElementsByTagName("attachment").item(0); 
        if (attachmentEl != null) 
        { 
            writer.write("\nAttachment==>\n"); 
            cid = attachmentEl.getAttribute("href").substring(4);//get rid of cid: 
            writer.write("Content-ID = "+cid+"\n"); 
            MimeBodyPart attachment = requestContext.getBodyPart(cid); 
            try 
            { 
                writer.write("The attachment is...\n"+attachment.getContent()+"\n"); 
            }catch(Exception ex) 
            { 
                throw new SOAPException(Constants.FAULT_CODE_SERVER, 
                    "Error writing response", ex); 
            } 
        }else 
            writer.write("The Content-ID is null!\n"); 
    } 
System.out.println(writer.toString()); 
… … 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值