目录:
1、RPC请求样例
2、SOAP请求样例
3、完整请求样例
内容:
1、RPC请求样例
RPC请求Java代码:
2、SOAP请求样例
//service服务对象
Service service = (Service) Class.forName("org.apache.axis.client.Service").newInstance();
Call call = service.createCall();
//设置请求目标地址
call.setTargetEndpointAddress(RquestConstant.ENDPOINT);
QName qname = new QName(null,RquestConstant.METHOD_NAME);
//设置请求目标方法
call.setOperationName(qname);
//定义存储认证用户信息对象
Map<String, String> header = new Hashtable<String, String>();
header.put(Call.USERNAME_PROPERTY, RquestConstant.PUBLIC_USER);
header.put(Call.PASSWORD_PROPERTY, RquestConstant.PUBLIC_PASSWORD);
//将认证信息放入调用对象的属性中
call.setProperty(HTTPConstants.REQUEST_HEADERS, header);
Object ret = call.invoke(arguments);
//打印调用结果
System.out.println(ret);
SOAP请求Java代码:
//SOAP连接工厂
SOAPConnectionFactory soapConnFactory = SOAPConnectionFactory.newInstance();
//soap工厂
SOAPFactory soapFactory = SOAPFactory.newInstance();
//消息工厂
MessageFactory factory = MessageFactory.newInstance();
SOAPBody soapBody = null;
SOAPConnection conn = null;
try{
//创建soap连接
conn = soapConnFactory.createConnection();
//创建消息
SOAPMessage message = factory.createMessage();
//读取消息构件
SOAPPart soapPart = message.getSOAPPart();
//读取消息壳
SOAPEnvelope envelope = soapPart.getEnvelope();
//读取消息体
SOAPBody body = envelope.getBody();
//请求方法对象
Name requestMethodName = soapFactory.createName(RquestConstant.METHOD_NAME);
//将请求方法对象添加到消息体中
SOAPBodyElement requestMethodElement = body.addBodyElement(requestMethodName);
//为请求构造参数对象
Name parameterIn = soapFactory.createName("in0");
//将参数添加到请求方法对象中
SOAPElement parameterEle = requestMethodElement.addChildElement(parameterIn);
//赋值给参数
parameterEle.addTextNode(arguments);
//添加用户认证信息
MimeHeaders hd = message.getMimeHeaders();
hd.addHeader(Call.USERNAME_PROPERTY, RquestConstant.PUBLIC_USER);
hd.addHeader(Call.PASSWORD_PROPERTY, RquestConstant.PUBLIC_PASSWORD);
//使所有更改生效
message.saveChanges();
//将此消息对象写入给定输出流
message.writeTo(System.out);
//包装目标地址
URL endpoint = new URL(RquestConstant.ENDPOINT);
//发生请求
SOAPMessage response = conn.call(message, endpoint);
//读取结果
soapBody = response.getSOAPBody();
}catch(Exception e){
throw e;
}finally{
conn.close();
}
System.out.println("消息内容:" + soapBody);
3、完整请求样例
见附件,所需要jar包与Web Service附加身份认证样例jar包相同。