简体   繁体   中英

How to do Preemptive Authentication in SOAP Based Web Service Client

I am working on a project in which i need to create a Web Service Client Element. I have used SAOPFACTORYCONNECTION to create simple Web Service Client Element. I am successfully able to create it , Here is code for that Client.

//import java.util.Date;

//import com.audium.server.AudiumException;
//import com.audium.server.session.ActionElementData;
//import com.audium.server.voiceElement.ActionElementBase;
//import com.ef.clients.warid.custom.Repository;

import javax.xml.soap.*;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;

//import com.ef.clients.warid.ucip.UcipUtility;

/**
 * @author 
 * 
 */
public class MainClass {


    public static void main (String[] arg)
    {

        // Repository repo=new Repository();

//      String splan = (String) data.getSessionData("SPLANID");
//      String ani = (String) data.getSessionData("ANI");
        String ani="03342323232";
        String splan="dfdf";
        String response_code = "";

        try {

            SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
            SOAPConnection soapConnection = soapConnectionFactory.createConnection();
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();


            String url="http://192.108.100.47:9090/CBR/services/callerStatus?wsdl"  ;
            SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(ani, splan), url);

            System.out.println("So after the URL ");
            SOAPBody soapBody = soapResponse.getSOAPBody();

            Source sourceContent = soapResponse.getSOAPPart().getContent();

            StreamResult result = new StreamResult(System.out);
            transformer.transform(sourceContent, result);

//          while (iterator.hasNext()) {
//              System.out.println("In the loop");
//              SOAPBodyElement bodyElement = (SOAPBodyElement) iterator.next();
//              response_code = bodyElement.getTextContent();
//          //  System.out.println("Response code is "+response_code);
//          }

            soapConnection.close();

        } catch (Exception ex)
        {
            System.out.println("Here is the exception "+ex.toString());

        } finally {

            // unica.cleanRepositories();
        }
    }

    public static SOAPMessage createSOAPRequest(String subno, String splan)
            throws Exception {
        MessageFactory messageFactory = MessageFactory.newInstance();
        SOAPMessage soapMessage = messageFactory.createMessage();

        SOAPPart soapPart = soapMessage.getSOAPPart();


        // SOAP Envelope
        SOAPEnvelope envelope = soapPart.getEnvelope();
//      setOptionalHeaders(soapMessage, envelope);
        envelope.addNamespaceDeclaration("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
        envelope.addNamespaceDeclaration("ws", "http://ws.services.cbr.ef.com/");

            // SOAP Body
        SOAPBody soapBody = envelope.getBody();
        SOAPElement GetTagStatus=  soapBody.addChildElement("getCallerStatus");

        SOAPElement ani = GetTagStatus.addChildElement("ani");

        ani.addTextNode("1234567");


        soapMessage.saveChanges();

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

        return soapMessage;
    }

}

This code is working perfectly fine, Now i need to make another client but in that client there is one different , In that client i need to add Preemptive Authentication. How can i do that. ?? This is my Question

If i access that Web Service URL in the SOAPUI tool, then i need to select Preeptive type from Auth and gave username password it work perfactly fine. Now i need to to same in Java code , how can i do that.

Check your SoapUI HTTP log. Look at what kind of headers SoapUI is sending etc.. I also was misled by need for "Preemptive Authentication" posted in a help section of one of the Web Services providers. In fact I needed to include SOAPAction header in a SOAP request - that solved the issue.

Yes, add the SOAPAction. I was also facing the same issue. In my case SOAPAction is the operation name.

MimeHeaders hd = soapMessage.getMimeHeaders();
hd.addHeader("SOAPAction", "getUserDetails");
String authorization = new sun.misc.BASE64Encoder().encode((username+":"+password).getBytes());
hd.addHeader("Authorization", "Basic " + authorization);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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