简体   繁体   English

如何使用Java在SOAP UI中使用用户名和密码发送SOAP请求

[英]How to send SOAP request with username and password like in SOAP UI using java

Sorry if this question is duplicated, but I tried a couple of solutions in StackOverflow and still get issue with sending SOAP request. 抱歉,如果重复此问题,但我在StackOverflow中尝试了几种解决方案,但在发送SOAP请求时仍然遇到问题。

My current situation is my java application needs to send SOAP request to my customer web service, which I did successfully in SOAP UI, with username, password, Authentication type: Preemptive. 我当前的情况是我的Java应用程序需要将SOAP请求发送到我的客户Web服务,这是我在SOAP UI中成功完成的,使用用户名,密码,身份验证类型:Preemptive。 Please see the picture 请看图片

在此处输入图片说明

Now I want to do this in Java, I've already created the SOAP envelope, without setting password, the response is exactly the same as in SOAP UI using no authentication, it means the java code only needs to add authentication to work. 现在,我想在Java中执行此操作,我已经创建了SOAP信封,没有设置密码,其响应与不使用身份验证的SOAP UI中的响应完全相同,这意味着Java代码仅需要添加身份验证即可工作。 Here is my current Java code: 这是我当前的Java代码:

callSoapWebService(soapEndpointUrl, soapAction);

private static void callSoapWebService(String soapEndpointUrl, String soapAction) {
    try {
        // Create SOAP Connection
        SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
        SOAPConnection soapConnection = soapConnectionFactory.createConnection();

        // Send SOAP Message to SOAP Server
        SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(soapAction), soapEndpointUrl);

        // Print the SOAP Response
        System.out.println("Response SOAP Message:");
        soapResponse.writeTo(System.out);
        System.out.println();

        soapConnection.close();
    } catch (Exception e) {
        System.err.println("\nError occurred while sending SOAP Request to Server!\nMake sure you have the correct endpoint URL and SOAPAction!\n");
        e.printStackTrace();
    }
}

private static SOAPMessage createSOAPRequest(String soapAction) throws Exception {
    MessageFactory messageFactory = MessageFactory.newInstance();
    SOAPMessage soapMessage = messageFactory.createMessage();

    createSoapEnvelope(soapMessage);

    MimeHeaders headers = soapMessage.getMimeHeaders();
    headers.addHeader("SOAPAction", soapAction);

    soapMessage.saveChanges();

    return soapMessage;
}

Inside createSoapEnvelope() is code to create SOAP request body, so I guess I have to do something with the header. 在createSoapEnvelope()内部是用于创建SOAP请求主体的代码,因此我想我必须对标头做一些事情。 Anyone can help me to achieve the authentication like what I did in SOAP UI? 任何人都可以像在SOAP UI中一样帮助我实现身份验证?

You need to send the username and password in Basic format: 您需要以基本格式发送用户名和密码:

String userAndPassword = String.format("%s:%s",username, password);

String basicAuth = new sun.misc.BASE64Encoder().encode(userAndPassword.getBytes());
MimeHeaders mimeHeaders = message.getMimeHeaders();
mimeHeaders.addHeader("Authorization", "Basic " + basicAuth);

If you are using Java 8 use Base64.getEncoder().encodeToString() instead of new sun.misc.BASE64Encoder() . 如果您使用的是Java 8,请使用Base64.getEncoder().encodeToString()而不是new sun.misc.BASE64Encoder()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM