`
fangzhu19880123
  • 浏览: 30029 次
  • 性别: Icon_minigender_2
  • 来自: 上海
社区版块
存档分类
最新评论

Webservice包含请求头与附件的接口调用(Axis实现)

阅读更多
工作需要,与电信做彩信接口,使用webservice,需要发送附件,认证信息放置于请求头中。
其中,附件DataHandler是作为附件发送,而不是作为某个请求参数发送。

这块东西研究了几天,总算是可以发送了,测试了下接收,也是ok的,记录下。

请求:
// 接口调用
Service service = new Service();
Call call = (Call) service.createCall();
String url = PropertiesManage.getValue("url");
System.out.println("接口地址: " + url);

call.setTargetEndpointAddress(new URL(url));
call.setOperationName(new QName("TestService", "sendMessage"));

// 添加头信息
String spId = PropertiesManage.getValue("message.header.spId");
QName endReason = new QName("TestService", "EndReason");
call.registerTypeMapping(EndReason.class, endReason, BeanSerializerFactory.class, BeanDeserializerFactory.class);
Name headerName = new PrefixedQName(new QName("RequestSOAPHeader"));
SOAPHeaderElement head = new SOAPHeaderElement(headerName);
head.setActor("");
SOAPElement spIdEle = head.addChildElement("spId"); 
// 此处省略其他头信息
call.addHeader(head);

// 请求参数格式
QName addresses = new QName("TestService", "URI[]");
call.registerTypeMapping(URI[].class, addresses, ArraySerializerFactory.class, ArrayDeserializerFactory.class);
// 此处省略其他请求参数

call.addParameter("addresses", addresses, ParameterMode.IN);
// 此处省略其他请求参数
call.setReturnType(XMLType.XSD_STRING);

// 短信接收者
URI[] uris = new URI[args.length];
for (int i = 0; i < args.length; i++) {
	uris[i] = new URI(args[i]);
	System.out.println("消息接收号码: " + args[i]);
}

// 此处省略其他请求参数

// 彩信内容,添加附件
if ("on".equals(PropertiesManage.getValue("message.need"))) {
	String fileName = PropertiesManage.getValue("message.content");
	System.out.println("彩信内容: " + fileName);
	// 由于我获得的是内容的byte[],下面模拟下
	// DataHandler dh = new DataHandler(new FileDataSource(fileName));

	// 获得的是byte[]
	File file = new File(fileName);
	byte[] bs = null;
	if (file != null) {
		FileInputStream fis = new FileInputStream(file);
		if (fis != null) {
			int len = fis.available();
			bs = new byte[len];
			fis.read(bs);
		}
	}
	// 由byte[]转换为DataHandler
	DataHandler dh = new DataHandler(new ByteArrayDataSource(bs, "application/octet-stream"));
	AttachmentPart ap= new AttachmentPart(dh);
	ap.setContentId("content");
	call.addAttachmentPart(ap);
}

System.out.println("消息发送中...");
// 上面省略了不少参数,如果直接copy,此处报错是必然的
Object ret = call.invoke(new Object[]{uris, sender, sub, MessagePriority.Default, info, request});

if (null == ret) {
	System.out.println("Received null ");
	throw new AxisFault("", "Received null", null, null);
}

if (ret instanceof String) {
	System.out.println("Received problem response from server: " + ret);
}



模拟的处理:
MessageContext msgContext = MessageContext.getCurrentContext();
Message reqMsg = msgContext.getRequestMessage();
	
FileOutputStream fos = null;
InputStream is = null;
try {
	// 获取请求头放置于Map中,由于是模拟,未做特殊处理
	SOAPHeader heaader = reqMsg.getSOAPHeader();
	SOAPElement element = (SOAPElement)heaader.getChildElements().next();
	Map<String, Object> map = new HashMap<String, Object>(); 
	for (Iterator it = element.getChildElements(); it.hasNext();) {
		SOAPElement ele = (SOAPElement) it.next();
		map.put(ele.getLocalName(), ele.getValue());
	}
	
	// 获取附件信息
	Attachments attachments = reqMsg.getAttachmentsImpl();
	if (null == attachments) {
		System.out.println("no attachments");
	} else {
		Part p = attachments.getAttachmentByReference("content");
		if (null == p) {
			System.out.println("no attachment, contentid = content");
		} else {
			DataHandler dh = ((AttachmentPart) p).getDataHandler();
			fos = new FileOutputStream("C:/Users/Public/Pictures/Sample Pictures/3.jpg");
			int n = 0;
			is = dh.getInputStream();
			System.out.println(is.available());
			while ((n = is.read()) != -1) {
				fos.write(n);
			}
			System.out.println("write to file C:/Users/Public/Pictures/Sample Pictures/3.jpg");
		}
	}
} catch (Exception e) {
	e.printStackTrace();
} finally {
	if (fos != null) {
		try {
			fos.close();
		} catch (IOException e) {
		}
	}
	if (is != null) {
		try {
			is.close();
		} catch (IOException e) {
		}
	}
}
return "0k";
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics