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";
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值