0

I have a webservice whose url is something like this:

http://pc212323/AdminService/HelloWorldService.asmx?wsdl

Now when i hit this service from soapUI it asks for Username, Password in a saperate popup window. This is done before soapUI loads the request structure. Again then i have to pass Username and password in the Request Properties present on the left of SoapUI tool. Then it gives me the output.

Now i want to hit this asmx sevice through java but am unable to figure out how to invoke this service by passing username and password.

In short i want to replicate this behaviour of soapUI through java code

Looking forward to your answers. Thanks in advance.

I am invoking the above service with this code of mine:

import javax.xml.namespace.QName;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.transport.http.HTTPConstants;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;


public class HelloWorldSecurity {

                public static void main(String[] args) {
                                HelloWorldSecurity helloWorldSecurity = new HelloWorldSecurity ();
                                helloWorldSecurity.service_code();
                }

                public ServiceClient configuration()
                {              
                                                System.out.println("configuration");
                                ServiceClient stClient = null;
                                try{
                                                org.apache.axis2.context.ConfigurationContext configurationContext = org.apache.axis2.context.ConfigurationContextFactory.createConfigurationContextFromFileSystem(null,null);
                                                MultiThreadedHttpConnectionManager multiThreadedHttpConnectionManager = new MultiThreadedHttpConnectionManager();
                                                HttpConnectionManagerParams params = new HttpConnectionManagerParams();
                                                params.setDefaultMaxConnectionsPerHost(2);
                                                multiThreadedHttpConnectionManager.setParams(params);
                                                HttpClient httpClient = new       HttpClient(multiThreadedHttpConnectionManager);
                                                configurationContext.setProperty(HTTPConstants.CACHED_HTTP_CLIENT,httpClient);
                                                stClient = new ServiceClient(configurationContext , null);
                                                Options options = new Options();
                                                EndpointReference targetEPR = new EndpointReference("http://pc212323/AdminService/HelloWorldService.asmx");
                                                options.setTo(targetEPR);
                                                options.setAction("http://tempuri.org/HelloWorld");
                                                options.setUserName("kevin");
                                                options.setPassword("password-1");
                                                stClient.setOptions(options);
                                }
                                catch(Exception e)
                                {
                                                e.printStackTrace();
                                }
                                return stClient;
                }

                public void service_code(){
                                OMFactory fac = OMAbstractFactory.getOMFactory();
                                OMNamespace ns = fac.createOMNamespace("http://tempuri.org", "ns1");
                                OMElement result = null;
                                OMElement PayloadElement = fac.createOMElement("HelloWorld", ns);
                                ServiceClient client = configuration();
                                try {
                                     result = client.sendReceive(PayloadElement);
                                } catch (AxisFault e) {
                                                e.printStackTrace();
                                }
                }

}

But i am getting error as:

org.apache.axis2.AxisFault: Transport error: 401 Error: Unauthorized
    at org.apache.axis2.transport.http.HTTPSender.handleResponse(HTTPSender.java:310)
    at org.apache.axis2.transport.http.HTTPSender.sendViaPost(HTTPSender.java:194)
    at org.apache.axis2.transport.http.HTTPSender.send(HTTPSender.java:75)
    at org.apache.axis2.transport.http.CommonsHTTPTransportSender.writeMessageWithCommons(CommonsHTTPTransportSender.java:404)
    at org.apache.axis2.transport.http.CommonsHTTPTransportSender.invoke(CommonsHTTPTransportSender.java:231)
    at org.apache.axis2.engine.AxisEngine.send(AxisEngine.java:443)
    at org.apache.axis2.description.OutInAxisOperationClient.send(OutInAxisOperation.java:406)
    at org.apache.axis2.description.OutInAxisOperationClient.executeImpl(OutInAxisOperation.java:229)
    at org.apache.axis2.client.OperationClient.execute(OperationClient.java:165)
    at org.apache.axis2.client.ServiceClient.sendReceive(ServiceClient.java:555)
    at org.apache.axis2.client.ServiceClient.sendReceive(ServiceClient.java:531)
    at HelloWorldSecurity.service_code(HelloWorldSecurity.java:63)
    at HelloWorldSecurity.main(HelloWorldSecurity.java:22)
John Topley
  • 113,588
  • 46
  • 195
  • 237
Roy
  • 1,231
  • 1
  • 24
  • 61
  • 2
    have you decided what you'll be using to generate the web service proxy? Last time I did this I think I used Axis2.. The authentication method you describe is likely "Basic Authentication". Once you decide on your stack then just Google for that and "client" and "basic authentication" and you'll be all set. – mikey Aug 16 '13 at 12:29
  • 1
    You probably need Basic auth, dependening on your HTTP library, you must pass this as instructed by the lib. – Michael-O Aug 16 '13 at 12:30
  • I am also using Axis2 to invoke the above service – Roy Aug 16 '13 at 12:34
  • I have updated my question. Please have a look – Roy Aug 16 '13 at 12:51

0 Answers0