HTTP Basic Authentication for WEBService call

15,421
 HttpTransportProperties.Authenticator
                       auth = new HttpTransportProperties.Authenticator();
            auth.setUsername("username");
            auth.setPassword("password");

 _serviceClient.getOptions().setProperty(org.apache.axis2.transport.http.HTTPConstants.BASIC_AUTHENTICATE,auth);
Share:
15,421
Java Guy
Author by

Java Guy

Software developer, primarily into web application development using Java, JEE http://myjavastuffs.blogspot.com/

Updated on June 04, 2022

Comments

  • Java Guy
    Java Guy almost 2 years

    I trying to invoke a web service, which has an Basic HTTP Authentication. I generated the client code using the WSDL2JAVA tool from AXIS.

    But I am not able to set the username and password to the webservice call.

    I tried to have them in the endpoint url as

    http://username:password@somwserver/wsdl

    But I am getting the unauthorized error for this. I am trying to figure out a way to get this set to my call in the Java code....

    Note : I am able to invoke the same service via the soapUI and get the results. I provided the username and password in the "Aut" tab on the request.

    Here is some of the code snippets of my Stub, if this is userful for you

           _serviceClient = new org.apache.axis2.client.ServiceClient(configurationContext,_service);
    
    
        _serviceClient.getOptions().setTo(new org.apache.axis2.addressing.EndpointReference(
                targetEndpoint));
        _serviceClient.getOptions().setUseSeparateListener(useSeparateListener);
    
            //adding SOAP soap_headers
         _serviceClient.addHeadersToEnvelope(env);
        // set the message context with that soap envelope
        _messageContext.setEnvelope(env);
    
        // add the message contxt to the operation client
        _operationClient.addMessageContext(_messageContext);
    
        //execute the operation client
        _operationClient.execute(true);
    

    Any inputs will be greatly appreciated!!

  • Java Guy
    Java Guy over 13 years
    Thanks! This worked. I found only this though _serviceClient.getOptions().setProperty(org.apache.axis2.tra‌​nsport.http.HTTPCons‌​tants.AUTHENTICATE,a‌​uth);
  • Kaitsu
    Kaitsu over 12 years
    Does someone know why the credentials cannot be put into the endpoint url as shown in the question? You can do it with Apache HttpClient and I guess that Axis is using that behind the scenes...