2013-09-27 163 views
1

我提供了下面的示例請求,但我不知道如何實際執行它。使用SOAP的Java Web服務調用

POST /InfoTransit/userservices.asmx HTTP/1.1 
Host: 10.0.2.52 
Content-Type: text/xml; charset=utf-8 
Content-Length: length 
SOAPAction: "http://miz.it/infotransit/GetBusStopsList" 

<?xml version="1.0" encoding="utf-8"?> 
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
    <soap:Body> 
    <GetBusStopsList xmlns="http://miz.it/infotransit"> 
     <auth> 
     <user>..of course, I have this..</user> 
     <password>..and this..</password> 
     </auth> 
    </GetBusStopsList> 
    </soap:Body> 
</soap:Envelope> 

我試圖寫一個Java客戶端...

import javax.xml.soap.*; 
import javax.xml.transform.*; 
import javax.xml.transform.stream.*; 

public class SOAPClientSAAJ { 

    /** 
    * Starting point for the SAAJ - SOAP Client Testing 
    */ 
    public static void main(String args[]) { 
     try { 
      // Create SOAP Connection 
      SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance(); 
      SOAPConnection soapConnection = soapConnectionFactory.createConnection(); 

      // Send SOAP Message to SOAP Server 
      String url = "http://miz.it/InfoTransit/userservices.asmx"; 
      SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url); 

      // Process the SOAP Response 
      printSOAPResponse(soapResponse); 

      soapConnection.close(); 
     } catch (Exception e) { 
      System.err.println("Error occurred while sending SOAP Request to Server"); 
      e.printStackTrace(); 
     } 
    } 

    private static SOAPMessage createSOAPRequest() throws Exception { 
     MessageFactory messageFactory = MessageFactory.newInstance(); 
     SOAPMessage soapMessage = messageFactory.createMessage(); 
     SOAPPart soapPart = soapMessage.getSOAPPart(); 

     String serverURI = "http://miz.it/infotransit/GetBusStopsList"; 

     // SOAP Envelope 
     SOAPEnvelope envelope = soapPart.getEnvelope(); 

     // SOAP Body 
     SOAPBody soapBody = envelope.getBody(); 
     SOAPElement getBusStopsList = soapBody.addChildElement("GetBusStopsList xmlns='http://miz.it/infotransit'"); 
     SOAPElement auth = getBusStopsList.addChildElement("auth"); 
     SOAPElement user = auth.addChildElement("user"); 
     user.addTextNode(" .... "); 
     SOAPElement password = auth.addChildElement("password"); 
     password.addTextNode(" ... "); 


     MimeHeaders headers = soapMessage.getMimeHeaders(); 
     headers.addHeader("SOAPAction", serverURI); 
     headers.addHeader("Content-Type:", "text/xml; charset=uft-8"); 

     soapMessage.saveChanges(); 

     /* Print the request message */ 
     System.out.print("Request SOAP Message = "); 
     soapMessage.writeTo(System.out); 
     System.out.println(); 

     return soapMessage; 
    } 

    /** 
    * Method used to print the SOAP Response 
    */ 
    private static void printSOAPResponse(SOAPMessage soapResponse) throws Exception { 
     TransformerFactory transformerFactory = TransformerFactory.newInstance(); 
     Transformer transformer = transformerFactory.newTransformer(); 
     Source sourceContent = soapResponse.getSOAPPart().getContent(); 
     System.out.print("\nResponse SOAP Message = "); 
     StreamResult result = new StreamResult(System.out); 
     transformer.transform(sourceContent, result); 
    } 

} 

不過,我得到一個連接超時。另外,輸出SOAP消息的信封不完全匹配示例請求的包絡。

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> 

客戶端有問題嗎?或者它是服務提供商?

回答

0

你可以試試這個在您的代碼:

SOAPElement getBusStopsList = soapBody.addChildElement("GetBusStopsList", "", "http://miz.it/infotransit"); 

編輯:第一個字母是getBusStopsList小寫。所以,請嘗試GetBusStopsList

這裏是新代碼:

SOAPElement GetBusStopsList = soapBody.addChildElement("GetBusStopsList", "", "http://miz.it/infotransit"); 
+0

不良反應:(400Bad請求.....在SOAPClientSAAJ.main(SOAPClientSAAJ.java:18) – Skogen

+0

你解決這個問題? – herry