2012-11-28 32 views
12

Tôi tự hỏi có cách nào để chuyển đổi chuỗi thành SOAPMessage không?Làm thế nào để chuyển đổi một chuỗi thành SOAPMessage trong Java?

Hãy để tôi nói tôi có một chuỗi như sau:

String send = "<soap:Envelope xmlns:mrns0=\"http://sdp.SOMETHING.com/mapping/TSO\" xmlns:sdp=\"http://sdp.SOMETHING.com.tr/mapping/generated\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">" 
     + "<soap:Header>" 
     + "<sdp:token>" 
     + "<sdp:sessionId>" + sessionId + "</sdp:sessionId>" 
     + "</sdp:token>" 
     + "<sdp:transaction-list>" 
     + "<sdp:transaction-id>" + 11 + "</sdp:transaction-id>" 
     + "</sdp:transaction-list>" 
     + "</soap:Header>" 
     + "<soap:Body>" 
     + "<sdp:SendSMSInput>" 
     + "<sdp:EXPIRY_DATE>" + extime + "</sdp:EXPIRY_DATE>" 
     + "<sdp:MESSAGE_CLASS>0</sdp:MESSAGE_CLASS>" 
     + "<sdp:S_DATE>" + time + "</sdp:S_DATE>" 
     + "<sdp:SHORT_NUMBER>1905</sdp:SHORT_NUMBER>" 
     + "<sdp:SRC_MSISDN>" + numSend + "</sdp:SRC_MSISDN>" 
     + "<sdp:TO_RECEIVERS>" 
     + "<sdp:msisdn>" + numSend + "</sdp:msisdn>" 
     + "</sdp:TO_RECEIVERS>" 
     + "<sdp:MESSAGE_BODY>" 
     + "<sdp:message>Message body here.</sdp:message>" 
     + "</sdp:MESSAGE_BODY>" 
     + "</sdp:SendSMSInput>" 
     + "</soap:Body>" 
     + "</soap:Envelope>"; 


Khi tôi làm điều này:

SOAPConnectionFactory sfc = SOAPConnectionFactory.newInstance(); 
    SOAPConnection connection = sfc.createConnection(); 
    InputStream is = new ByteArrayInputStream(send.getBytes()); 
    SOAPMessage request = MessageFactory.newInstance().createMessage(null, is); 
    request.removeAllAttachments(); 

    SOAPPart part = request.getSOAPPart(); 
    part.detachNode(); 
    SOAPEnvelope env = part.getEnvelope(); 
    env.detachNode(); 
    SOAPBody body = env.getBody(); 
    body.detachNode(); 
    SOAPHeader head = env.getHeader(); 
    head.detachNode(); 

    request.writeTo(System.out); 

    URL endpoint = new URL("http://sdp.somewhere.com.tr/view/LbsOpaqueService.wsdl"); 
    SOAPMessage response = connection.call(request, endpoint); 
    System.out.println(response.getContentDescription()); 

Tất cả mọi thứ hoạt động tốt. Nhưng tôi vẫn nhận được NULL từ máy chủ. Điều gì có thể là lý do? Tôi đã thay đổi < và> để thoát khỏi ký tự.

+1

Bạn có thể phân tích nó. Hoặc xây dựng nó như một tin nhắn ở nơi đầu tiên. –

Trả lời

26

Chuyển đổi chuỗi thành luồng đầu vào, sau đó đọc chuỗi đó vào nhà máy thông báo SOAP.

InputStream is = new ByteArrayInputStream(send.getBytes()); 
SOAPMessage request = MessageFactory.newInstance().createMessage(null, is); 

You can read about how to do this here.

+0

Cảm ơn rất nhiều. Một câu hỏi nữa: Khi tôi tạo SOAPMessageRequest thì nó sẽ tự động tạo tiêu đề và phần thân. Tôi có phải thay đổi một số phần của chuỗi không? –

+0

Đã xong, thưa bạn! Nhưng tôi nhầm lẫn về phần nào cần xóa. –

2

này làm việc cho tôi:

SOAPMessage sm = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL).createMessage(new MimeHeaders(), is); 
+1

Tôi đã cố gắng trong ba ngày, cố gắng mọi kết hợp có thể, cố gắng gỡ lỗi mỗi byte. Và luôn luôn nhận được lỗi "ngoại lệ con trỏ null" Cuối cùng tôi đã thử đề xuất của bạn và nó đã hoạt động –

0

Thay vì:

System.out.println(response.getContentDescription()); 

Thay thế nó bằng sau dòng sau khi nhận được câu trả lời:

response.writeTo(System.out); 
0

Bằng cách này bạn có thể đọc từ một tập tin:

byte[] encoded = Files.readAllBytes(Paths.get("C:/resources/soap/RequestFileReply.xml")); 
InputStream bStream = new ByteArrayInputStream(encoded); 
SOAPMessage request = MessageFactory.newInstance().createMessage(null, bStream); 
SOAPBody body = request.getSOAPBody(); 
Các vấn đề liên quan