Monday, 4 April 2016

How to use HTTP Authentication in Axis 1x client stubs ?


If your web service requires you to use HTTP Basic Authentication, you can adjust your Axis client code.

Example
Here we have generated locator class, service stub class and calling its operation "GetStockInfo".
StockInfoServiceLocator locator = new StockInfoServiceLocator();
StockInfoService service = locator.getStockService();

// Set user/password because basic authentication is required by Web Service
((Stub) service).setUsername("user name");
((Stub) service).setPassword("password");
-------------- OR ----------------
// to use Basic HTTP Authentication
((Stub) service)._setProperty(Call.USERNAME_PROPERTY, "user name");
((Stub) service)._setProperty(Call.PASSWORD_PROPERTY, "password");

// Call web service method
service.GetStockInfo("FOO");


Using Session-Based Authentication
If your web service requires you to use session-based Authentication (which requires you to pass a token as an HTTP Cookie), you can adjust your Axis client code like, 

StockInfoServiceLocator locator = new StockInfoServiceLocator();
StockInfoService service = locator.getStockService();
// to set a cookie
((Stub) service)._setProperty(Call.SESSION_MAINTAIN_PROPERTY, new Boolean(true));
((Stub) service)._setProperty(HTTPConstants.HEADER_COOKIE, "AuthToken=abc123");
service.GetStockInfo("FOO");

No comments:

Post a Comment

Note: only a member of this blog may post a comment.