统计 |
blog名称:人在旅途 日志总数:175 评论数量:505 留言数量:13 访问次数:1670177 建立时间:2005年12月7日 |
生命是过客,人在旅途。奶奶是信基督教的,没啥文化,却养育了四子二女,还带过九个孙辈。老人家对生命的看法就是“人都是客人,迟早要回去的。”就以《人在旅途》来纪念她。

« | September 2025 | » | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | | | | | |
|
公告 |
本人上传的源程序中可能引用或使用了第三方的库或程序,也可能是修改了第三方的例程甚至是源程序.所以本人上传的源程序禁止在以单纯学习为目的的任何以外场合使用,不然如果引起任何版权问题,本人不负任何责任. | |

|
本站首页 管理页面 写新日志 退出
调整中...
[Java技术]How to attach a user data(byte[]) to a soap message? |
人在旅途 发表于 2006/1/22 8:29:49 | 1. You will need following JAR lib: 1.saaj-api.jar: From J2ee lib; 2.saaj-impl.jar: From J2ee lib; 3.activation.jar: From J2ee lib; 4.soap.jar: From Apache soap project. url:http://www.apache.org/dyn/closer.cgi/ws/soap/ The jar file is small(about 250K), you can use it directly(Tested); Or you can get the source of class ByteArrayDataSource and edit it as your own class, becuase we only need this class in that soap.jar to put the user data(byte[]) into the soapmessage's attachment part(This way has not been tested yet.).2.Sample source code: // Connection to send messages. SOAPConnection con=null; try { SOAPConnectionFactory scf = SOAPConnectionFactory.newInstance(); con = scf.createConnection(); } catch(Exception e) { return; } try { // Create a message factory. MessageFactory mf = MessageFactory.newInstance();
// Create a message from the message factory. SOAPMessage msg = mf.createMessage();
// Message creation takes care of creating the SOAPPart - a // required part of the message as per the SOAP 1.1 // specification. SOAPPart sp = msg.getSOAPPart();
// Retrieve the envelope from the soap part to start building // the soap message. SOAPEnvelope envelope = sp.getEnvelope();
// Create a soap header from the envelope. SOAPHeader hdr = envelope.getHeader();
// Create a soap body from the envelope. SOAPBody bdy = envelope.getBody(); // Add a soap body element to the soap body SOAPBodyElement gltp = bdy.addBodyElement(envelope.createName("CIPHER", "m", "CIPHER.COM")); gltp.setAttribute("FUNCTION","ENCIPHER"); gltp.setAttribute("DIGEST","TRUE"); gltp.setAttribute("PASSWORD","1234567"); byte[] srcData = new byte[127]; for(int i=0;i<127;i++) srcData[i]=(byte)(i%256); AttachmentPart attach = msg.createAttachmentPart(); ByteArrayDataSource binSrc = new ByteArrayDataSource(srcData,"application/octet-stream"); DataHandler dataH = new DataHandler(binSrc); attach.setDataHandler(dataH); attach.setContentId("DATA"); msg.addAttachmentPart(attach); msg.writeTo(System.out); URL urlEndpoint = new URL("http://localhost:8080/SecurityServer-WebModule/SecuritySAAJ"); // Send the message to the provider using the connection. SOAPMessage reply = con.call(msg, urlEndpoint);
if (reply != null) { reply.writeTo(System.out); } else { System.err.println("No reply"); return; } // Create a message from the message factory. msg = mf.createMessage();
// Message creation takes care of creating the SOAPPart - a // required part of the message as per the SOAP 1.1 // specification. sp = msg.getSOAPPart();
// Retrieve the envelope from the soap part to start building // the soap message. envelope = sp.getEnvelope();
// Create a soap header from the envelope. hdr = envelope.getHeader();
// Create a soap body from the envelope. bdy = envelope.getBody(); // Add a soap body element to the soap body gltp = bdy.addBodyElement(envelope.createName("CIPHER", "m", "CIPHER.COM")); gltp.setAttribute("FUNCTION","DECIPHER"); gltp.setAttribute("DIGEST","TRUE"); gltp.setAttribute("PASSWORD","1234567"); Iterator iterator = reply.getAttachments(); byte[] retData = null; byte[] retDigest = null; AttachmentPart attachRet; while(iterator.hasNext()) { attach = (AttachmentPart)iterator.next(); if(attach.getContentId().equals("DATA")) { ByteArrayInputStream bs = (ByteArrayInputStream)attach.getContent(); ByteArrayOutputStream os1 = new ByteArrayOutputStream(); int xx = bs.read(); while(xx>=0) { os1.write(xx); xx = bs.read(); } retData = os1.toByteArray(); } else if(attach.getContentId().equals("DIGEST")) { ByteArrayInputStream bs = (ByteArrayInputStream)attach.getContent(); ByteArrayOutputStream os1 = new ByteArrayOutputStream(); int xx = bs.read(); while(xx>=0) { os1.write(xx); xx = bs.read(); } retDigest = os1.toByteArray(); } } attach = msg.createAttachmentPart(); binSrc = new ByteArrayDataSource(retData,"application/octet-stream"); dataH = new DataHandler(binSrc); attach.setDataHandler(dataH); attach.setContentId("DATA"); msg.addAttachmentPart(attach); AttachmentPart attachD = msg.createAttachmentPart(); binSrc = new ByteArrayDataSource(retDigest,"application/octet-stream"); dataH = new DataHandler(binSrc); attachD.setDataHandler(dataH); attachD.setContentId("DIGEST"); msg.addAttachmentPart(attachD); msg.writeTo(System.out); urlEndpoint = new URL("http://localhost:8080/SecurityServer-WebModule/SecuritySAAJ"); // Send the message to the provider using the connection. SOAPMessage reply2 = con.call(msg, urlEndpoint);
if (reply2 != null) { reply2.writeTo(System.out); } else { System.err.println("No reply2"); return; } //check return iterator = reply2.getAttachments(); byte[] retData2 = null; while(iterator.hasNext()) { attach = (AttachmentPart)iterator.next(); if(attach.getContentId().equals("DATA")) { ByteArrayInputStream bs = (ByteArrayInputStream)attach.getContent(); ByteArrayOutputStream os1 = new ByteArrayOutputStream(); int xx = bs.read(); while(xx>=0) { os1.write(xx); xx = bs.read(); } retData2 = os1.toByteArray(); } } if(null==retData2) { System.err.println("No retData2"); return; } if(retData2.length!=127) { System.err.println("retData2 length err:"+retData2.length); return; } for(int k=0;k<127;k++) if(retData2[k]!=srcData[k]) { System.err.println("Tesult data not equals to the source data!"+k); return; } System.err.println("OK!!!!!!!!!!!!!!!!!!!"); }catch(Exception e){ return; }
|
阅读全文(3284) | 回复(0) | 编辑 | 精华 |
|