2014-09-19 225 views
0

我想在Java中生成“UsernameToken-28FE7B32CCC1AB2B22141113557641136”>以向SOAP Web服务发送请求。使用SoapUI和请求很容易,因为它会自动生成,但我怎么能在java中做到这一点?我目前正在从外部文件发送请求,我如何从Java执行此操作并自动生成UsernameToken?如何为SOAP请求生成UsernameToken?

这里是我的代码:

public class SampleHttpClient 
{ 
    public static String invokeWebService(String webServiceURL, 
String requestXMLPath) 
throws FileNotFoundException, Exception 
{ 
PostMethod post = null; 
HttpClient client = new HttpClient(); 

try 
{ 
    // Read the SOAP request from the file 
    StringBuffer requestFileContents = new StringBuffer(); 
    BufferedReader bufferedReader = new BufferedReader(new FileReader(requestXMLPath)); 
    String line = null; 

    while((line = bufferedReader.readLine()) != null) 
    { 
    requestFileContents.append(line); 
    } 

    post = new PostMethod(webServiceURL); 
    post.setRequestHeader("Accept","application/soap+xml,application/dime,multipart/related,text/*"); 
    post.setRequestHeader("SOAPAction", ""); 

    // Request content will be retrieved directly from the input stream 
    RequestEntity entity = new StringRequestEntity(requestFileContents.toString(), "text/xml", "UTF-8"); 

    post.setRequestEntity(entity); 


    // Returns a number indicating the status of response 
    int result = client.executeMethod(post); 
    String response = post.getResponseBodyAsString(); 
    //bufferedReader.close(); 
    return response; 
} 

finally 
{ 
    // Release current connection to the connection pool once you are done 
    post.releaseConnection(); 

} 
} 
} 

而且invokeWS代码:

public class SampleTest extends TestCase 
{ 
public void test() throws FileNotFoundException, Exception 
{ 
    // Web service URL #1 
    final String wsURL = "http://webservice"; 


    // Request XML path 
    final String requestXMLPath = "C:\\request.txt"; 



    //Invoke the Web service #1 
    String webServiceResponse = SampleHttpClient.invokeWebService(wsURL,requestXMLPath); 
    System.out.println("Response #1: " + webServiceResponse); 

} 
} 

这里是XML请求(修改后的安全问题):

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v2="http://webservice"> 
    <soapenv:Header><wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"><wsse:UsernameToken wsu:Id="UsernameToken-28FC7B32CCC1BB2B21141113657641136"><wsse:Username>username</wsse:Username><wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest">password=</wsse:Password><wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">gt/swtwet/ww==</wsse:Nonce><wsu:Created>2014-09-19T14:22:56.411Z</wsu:Created></wsse:UsernameToken></wsse:Security></soapenv:Header> 
    <soapenv:Body> 
     <v2:getThis1> 
     <v2:getThis12> 
      <v3:getThis13> 
       <v3:getThis10> 
        <v31:Id>31045243</v31:Id> 
        <v31:getThis1222>545345</v31:getThis1222> 
       </v3:getThis10> 
       <v3:Number>1234124</v3:Number> 
      </v3:getThis14323> 
     </v2:getThis1343> 
     </v2:getThis112> 
    </soapenv:Body> 
</soapenv:Envelope> 
+0

有在这篇文章中的一些信息。看看它是否可以帮助你:http://stackoverflow.com/questions/14435580/add-wsseusernametoken-in-soap-header – edubriguenti 2014-09-22 13:57:46

回答

3

正如Add wsse:UsernameToken in soap header

您可以添加一个用户名令牌的消息是这样的:

//create SOAP 
     SOAPConnectionFactory sfc = SOAPConnectionFactory.newInstance(); 
     SOAPConnection connection = sfc.createConnection(); 

     SOAPMessage soapMessage = MessageFactory.newInstance().createMessage(); 
     SOAPPart soapPart = soapMessage.getSOAPPart(); 
     SOAPEnvelope soapEnvelope = soapPart.getEnvelope(); 

     SOAPBody soapBody = soapEnvelope.getBody(); 
     SOAPElement Header = soapBody.addBodyElement(new QName("Header")); 

//attribute      
     SOAPElement Security= Header.addChildElement(new QName("Security")); 
     SOAPElement UsernameToken= Security.addChildElement(new QName("UsernameToken")); 
     SOAPElement Username= UsernameToken.addChildElement(new QName("Username")); 
     SOAPElement Password= UsernameToken.addChildElement(new QName("Password")); 

//enter the username and password 
Username.addTextNode("username"); 
Password.addTextNode("password"); 

//send the soap and print out the result 
URL endpoint = "http://localhost:8080/soap/getMessage?wsdl"; 
     SOAPMessage response = connection.call(soapMessage, endpoint); 

EDITED

如果你已经有了一个字符串的邮件,你应该首先创建SoapMessage对象,从string

使用下面的方法:

private SOAPMessage getSoapMessageFromString(String xml) throws SOAPException, IOException { 
    MessageFactory factory = MessageFactory.newInstance(); 
    SOAPMessage message = factory.createMessage(new MimeHeaders(), new ByteArrayInputStream(xml.getBytes(Charset.forName("UTF-8")))); 
    return message; 
} 
+0

谢谢,所以,我应该从我的请求中删除线,我发送令牌,用户名和密码?你的代码将添加令牌,用户和传递给请求? – 2014-09-25 15:48:07

+0

如果你已经有一个字符串中的消息,你应该首先从字符串中创建SoapMessage,然后改变你想要的属性。我添加了一些信息。 – edubriguenti 2014-09-25 16:57:55