我使用默认的 Eclipse 向导创建了一个 Web 服务客户端(文件>新建>其他并为 Web 服务客户端选择一个向导)。生成了类,我可以使用带有如下代码的服务方法:
import java.rmi.RemoteException;
import javax.xml.rpc.ServiceException;
import com.olsn.ws.ShipmentService_PortType;
import com.olsn.ws.ShipmentService_ServiceLocator;
public class wsClient {
public static void main(String[] args) throws ServiceException, RemoteException {
//To load the web service and to create the client
ShipmentService_ServiceLocator WS = new ShipmentService_ServiceLocator();
ShipmentService_PortType client = WS.getShipmentServicePort();
//To get Close status for Issue
System.out.println(client.getCloseDesc("35557"));
//To get the list of NetP projects
for (int i=1; i<client.getProjects().length;i++)
{System.out.println(client.getProjects()[i].toString());}
}
}
现在的问题是引入了用户名和密码,我不知道如何修改我的代码来访问服务器,我试图添加验证器:
import java.net.Authenticator;
import java.net.PasswordAuthentication;
Authenticator myAuth = new Authenticator()
{
@Override
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("user", "password".toCharArray());
}
};
Authenticator.setDefault(myAuth);
或更改生成的代理类的 _initShipmentServiceProxy() 方法
private void _initShipmentServiceProxy() {
try {
shipmentService_PortType = (new com.olsn.ws.ShipmentService_ServiceLocator()).getShipmentServicePort();
if (shipmentService_PortType != null) {
if (_endpoint != null)
{
((javax.xml.rpc.Stub)shipmentService_PortType)._setProperty("javax.xml.rpc.service.endpoint.address", _endpoint);
**((javax.xml.rpc.Stub)shipmentService_PortType)._setProperty(javax.xml.rpc.Stub.USERNAME_PROPERTY, "user");
((javax.xml.rpc.Stub)shipmentService_PortType)._setProperty(javax.xml.rpc.Stub.PASSWORD_PROPERTY, "password");**
}
else
_endpoint = (String)((javax.xml.rpc.Stub)shipmentService_PortType)._getProperty("javax.xml.rpc.service.endpoint.address");
}
}
catch (javax.xml.rpc.ServiceException serviceException) {}
}
但我继续收到这些错误:
org.apache.axis.utils.JavaUtils isAttachmentSupported
WARNING: Unable to find required classes (javax.activation.DataHandler and javax.mail.internet.MimeMultipart). Attachment support is disabled.
Exception in thread "main" AxisFault
faultCode: {http://schemas.xmlsoap.org/soap/envelope/}Client.Authentication
faultSubcode:
faultString: Access denied to operation getCloseDesc
请告知我应该改变什么?