`
fangzhu19880123
  • 浏览: 30501 次
  • 性别: 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";
分享到:
评论

相关推荐

    java axis 调用C# webService所需的jar包

    在这种情况下,Axis作为一个成熟的SOAP客户端框架,可以帮助Java开发者实现对C# WebService的调用。本文将详细讲解如何使用Java Axis库来调用C# WebService,并介绍所需的jar包。 首先,理解Java Axis的基本概念是...

    利用AXIS调用webservice所需JAR包

    在描述中提到的"利用AXIS,通过wsdl的url直接调用webservice接口时所需要的jar包",这些JAR文件主要包括以下组件: - **axis.jar**:这是AXIS的核心库,包含了处理SOAP消息、WSDL解析和生成Stub的主要类。 - **...

    Axis1发布webservice服务

    本项目是一个关于如何使用Axis1发布Web服务的示例,包含了必要的jar包和其他文件,可以直接运行。 首先,我们需要了解Web服务的基本概念。Web服务是一种基于互联网协议的软件,能够通过XML(eXtensible Markup ...

    webservice学习二之(1)axis2服务器端方式开发总结(附件含有项目)

    - 文章中的"webservice_portal"可能是一个包含Axis2服务器端开发项目的压缩包,可能包括服务类、WSDL文件、服务配置文件以及部署脚本等。 - 开发者可以参考源码了解具体的实现细节,同时,使用类似Eclipse、...

    WebService大讲堂之Axis2

    在C#中调用Axis2发布的Web服务时,同样可以使用Axis2提供的工具生成C#客户端代码。然后在C#项目中引入这些代码,并创建服务代理对象来访问Web服务的方法。 六、复合类型数据的传递 在Web服务中经常需要处理复合类型...

    手把手教你用axis1.4搭建webservice

    标题中的“手把手教你用axis1.4搭建webservice”是指一篇教程,旨在指导读者如何使用Axis1.4这个开源工具来创建和部署Web服务。Axis是Apache软件基金会的一个项目,它提供了一种简单的方式来实现Java到SOAP(Simple ...

    webservice框架axis的jar包

    5. **客户端调用**:在客户端,使用Axis生成的客户端代码或直接构建SOAP请求调用服务。 总结来说,Apache Axis是一个强大的Web服务框架,它简化了Java Web服务的开发、部署和使用,而`axisLib`压缩包中的jar文件则...

    java版WebService生成客户端和服务端的实例

    1. axis.jar:这是Apache Axis的核心库,包含了处理WebService请求和响应的主要类。 2. commons-discovery.jar:提供服务发现功能,帮助找到网络上的可用服务。 3. commons-logging.jar:日志处理库,用于记录Axis...

    DataHander.rar_axis datahand_web service _webservice

    标题 "DataHandler.rar_axis datahand_web service _webservice" 暗示了这是一个关于使用Axis框架来处理Web服务的项目,其中可能包含了客户端和服务端的数据处理组件。描述中提到的"webservices附件传输"指的是通过...

    axis2 webservice jar包

    - 客户端调用:可以使用生成的客户端stub类或通过SOAP请求直接调用服务。 7. **高级特性** - **MTOM与SwA**:优化大附件传输,减少网络带宽消耗。 - **WS-Security**:提供安全功能,如数字签名、加密等。 - **...

    axis webservice 开发需要的ZIP

    - **WSDL(Web服务描述语言)**:定义了Web服务的接口,包括服务的位置、提供的操作以及如何调用这些操作。 - **SOAP**:作为传输数据的协议,使用XML格式封装消息。 - **UDDI(统一描述、发现和集成)**:一种...

    axis jar包下载

    1. `axis.jar`:AXIS框架的主要实现,包含了处理SOAP请求和响应的核心类。 2. `jaxrpc.jar`:Java API for XML-RPC,提供了与远程过程调用(RPC)风格的Web服务交互的接口。 3. `saaj.jar`:SOAP with Attachments ...

    WebService服务器端实例

    SOAP消息通常以XML格式封装,包含头、体和可选的附件部分,用于传递请求和响应。 2. **WSDL(Web服务描述语言)**:WSDL是WebService的接口定义语言,它描述了服务的位置、可用操作以及如何调用这些操作。开发者...

    WebService所需Jar包

    7. **Axis, CXF, GlassFish Metro等WebService实现框架**:这些框架提供了一套完整的工具和服务,帮助开发者更方便地创建、部署和调用Web服务。例如,Apache Axis和CXF提供了代码生成工具,可以从WSDL文件自动生成...

    axis.jar,axis-saaj-1.4.jar

    2. **Web服务服务器端实现**: Axis支持动态Web服务发布,可以从Java类或接口直接生成服务端代码,简化了服务端开发过程。 3. **SOAP处理**: Axis处理SOAP(简单对象访问协议)消息,这是Web服务之间通信的标准...

    axis 1.4 所有jar包

    标签 "axis webservice java" 明确指出这些JAR文件与Java平台上的Web服务相关,特别是使用Axis框架。Axis 提供了从WSDL生成Java代码、从Java类生成WSDL、调用Web服务以及发布新服务的能力。 通过这些JAR包,开发者...

    axis.jar资源包

    Axis.jar中包含了处理SOAP请求和响应所需的类库,使得开发者能够方便地创建、发布和调用Web服务。 Axis.jar包内含的16个jar文件通常包括以下组件: 1. Axis的核心库:提供Web服务的基本框架,包括解析和生成SOAP...

Global site tag (gtag.js) - Google Analytics