8°

WebServiceUtils工具类(xfire)

import org.apache.commons.lang.ArrayUtils;
import org.codehaus.xfire.client.Client;
import org.codehaus.xfire.transport.http.CommonsHttpMessageSender;

import java.net.URL; import java.util.Map;

public class WebServiceUtil {

private static final Logger logger = LoggerFactory.getLogger(WebServiceUtil .class);

/**
 * 调用 webService WEB 服务
 * 
 * @param params
 * @param map 验证信息
 * @return Object
 */
//
public static Object postWebService(String url, String method, Object[] params,Map<String,String> map ) throws Exception {
	try {
		Client client = new Client(new URL(url));
		// 设置连接超时参数
		 client.setProperty(CommonsHttpMessageSender.HTTP_TIMEOUT,"16000");
		// 请求完成后关闭连接释放资源
		client.setProperty(CommonsHttpMessageSender.DISABLE_KEEP_ALIVE, "true");
		// 禁用HTTP请求头部参数EXPECT
		client.setProperty(CommonsHttpMessageSender.DISABLE_EXPECT_CONTINUE, "true");
		// 启用响应数据传输压缩
		client.setProperty(CommonsHttpMessageSender.GZIP_RESPONSE_ENABLED, Boolean.TRUE);
		// 启用请求数据传输压缩
		// client.setProperty(CommonsHttpMessageSender.GZIP_REQUEST_ENABLED,
		// Boolean.TRUE);
		// 启用GZIP传输压缩
		// client.setProperty(CommonsHttpMessageSender.GZIP_ENABLED, Boolean.TRUE);
		// 添加身份认证
		// client.addOutHandler(new AuthenticationOutHandler(serviceDefinition.getServiceToken(), serviceDefinition.getServiceIdentifier()));
		if (null!=map) {
			client.addOutHandler(new MySoapHeader(map.get("uname"),map.get("password"),map.get("namespaceUrl")));
		}
		Object[] results = client.invoke(method, params);
		
		if (results == null || ArrayUtils.contains(results, Boolean.FALSE)) {
			throw new Exception("返回数据为空");
		}
		return results[0];
	} catch (Exception e) {
		logger.error("postWebService Exception ",e);
	}
}

}

import org.codehaus.xfire.MessageContext;
import org.codehaus.xfire.handler.AbstractHandler;
import org.jdom.Element;
import org.jdom.Namespace;

public class MySoapHeader extends AbstractHandler {

	private String Uname;
	private String Password;
	private String url;

	public MySoapHeader(String uname, String password,String url) {
		this.Uname = uname;
		this.Password = password;
		this.url = url;
	}

	@Override	
	public void invoke(MessageContext context) throws Exception {
		final Namespace namespace = Namespace.getNamespace("test",url);
		Element header = new Element("header", namespace);
		Element authToken = new Element("MySoapHeader", namespace);// Token是名字
		Element token1 = new Element("Uname", namespace);

		token1.addContent(Uname);// token是值,密钥
		Element token2 = new Element("Password", namespace);
		token2.addContent(Password);
		authToken.addContent(token1);
		authToken.addContent(token2);
		header.addContent(authToken);
		context.getCurrentMessage().setHeader(header);
//		XMLOutputter outputter = null;
//		Format format = Format.getCompactFormat();
//		format.setEncoding("GB2312");
//		format.setIndent("    ");
//		outputter = new XMLOutputter(format);
//
//		outputter.output(header, new FileOutputStream("d:\\a.xml"));
	}
}

 

本文由【Lion华】发布于开源中国,原文链接:https://my.oschina.net/lion1220/blog/3157855

全部评论: 0

    我有话说: