简体   繁体   中英

How to send username password for web service authentication from client side

I have searched this forum and and searched the net but i am not able to find the solution to my problem.

I was given a WSDL file, using which i generated stubs with the help of axis1.4 . I have also created the XML response which is exactly the same as which the service requires, but when i try to access the service it says "Authorization failed".

Then i came to know the web service is protected with username password.

How should i send username password in my XML request.

Below is my code how i generate a request.

public class SearcUserFriendlyIsMain {

/**
 * @param args
 */
Tews6SoapBindingStub stub = null;
public void init(){
    if(null == stub) {
        try {

            URL url = null;
            String endPoint = "http://localhost:1111";

            try {
                url = new URL(endPoint);
            } catch (MalformedURLException e) {                 
                e.printStackTrace();
            }

            stub = new Tews6SoapBindingStub(url, null);
            stub.setMaintainSession(true);

        } catch (AxisFault e) {



            StringBuffer sb = new StringBuffer();
            for (StackTraceElement t : e.getStackTrace()) {
                sb.append("\n").append(t.toString());
            }
            System.out.println("DEBUG"+ sb.toString());

        }
    }
}
public static void main(String[] args) {
    SearcUserFriendlyIsMain tds = new SearcUserFriendlyIsMain();
    tds.init();
    NYCBSearchUserByFriendlyIdQuery query = new NYCBSearchUserByFriendlyIdQuery();
    NYCBSearchUserByFriendlyIdSearch search = new NYCBSearchUserByFriendlyIdSearch();
    NYCBSearchUserByFriendlyIdQueryResult res = null;
    NYCBSearchUserByFriendlyIdSearchFilter filter = new NYCBSearchUserByFriendlyIdSearchFilter();
    NYCBSearchUserByFriendlyIdSearchFilter[] filters = {filter};
    filter.setOp(OperatorType.EQUALS);
    filter.setIndex(new  BigInteger("1"));
    filter.setField("USERID");
    filter.setValue("junaidaj01");
    search.setFilter(filters);
    NYCBSearchUserByFriendlyIdQueryTaskContext context = new NYCBSearchUserByFriendlyIdQueryTaskContext();
    context.setAdmin_id("uid=00000001-0001-0001-0001-0000000ja202,ou=serviceaccounts,dc=test,dc=com");
    query.setNYCBSearchUserByFriendlyIdSearch(search);
    try {
        res = tds.stub.NYCBSearchUserByFriendlyIdQuery(context,query);
        System.out.println(res.getImsStatus());
    } catch (ImsException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (RemoteException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}

Using Basic HTTP Authentication If your web service requires you to use HTTP Basic Authentication, you can adjust your Axis client code like so:

// to use Basic HTTP Authentication:
((Stub) service)._setProperty(Call.USERNAME_PROPERTY, "user name");
((Stub) service)._setProperty(Call.PASSWORD_PROPERTY, "password");

Adding SOAP Header Elements Sometimes a web service will also require you to include SOAP Header information, usually for login purposes. In the case of explicit headers (that are defined in the WSDL), the SOAP Header information is supposed to be writable directly from the Axis client code. However, if the headers are implicit (not defined in the WSDL), you will have to generate them manually.

// add a <UserName> node to the SOAP Header
((Stub) service).setHeader("urn:thisNamespace", "UserName", "Kuntal Ganguly");

Let me know if this solves your issue?

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