所需要ksoap jar下载: http://download.csdn.net/detail/w123456h19/4676150
import java.util.HashMap;
import java.util.Map;import java.util.Map.Entry;
import org.apache.commons.lang.StringUtils;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
/**
* 调用WebService
* @author kewh 2012-07-24
*/
public class WebServiceHelper {
// 非.net提供的WebService
private Boolean dotNet = Boolean.FALSE;
// 请求响应时间(毫秒)
private Integer timeout = 60000;
public String callWebService(String methodName,Map<String, String> paramMap){
String result = null;
if(StringUtils.isNotBlank(methodName)){
String[] strs = callWebServices(methodName,paramMap);
if(null!=strs && strs.length>0){
result = strs[0];
}
}
return result;
}
public String[] callWebServices(String methodName,Map<String, String> paramMap){
String[] result = null;
try {
SoapObject soapObject = initSoapObject(methodName,paramMap);
if(null != soapObject && soapObject.getPropertyCount() > 0){
result = new String[soapObject.getPropertyCount()];
for(int i=0;i< soapObject.getPropertyCount();i++){
SoapPrimitive value=(SoapPrimitive) soapObject.getProperty(i);
result[i] = value.toString();
}
}
} catch (Exception ex) {
throw new RuntimeException(ex);
}
return result;
}
private SoapObject initSoapObject(String methodName , Map<String, String> paramMap) throws Exception {
String nameSpace = LoadProperties.getInstance().getPropertieValue("service_namespace");
String serviceAction = LoadProperties.getInstance().getPropertieValue("service_action");
SoapObject request = new SoapObject(nameSpace, methodName);
PropertyInfo propertyInfo = getPropertyInfo(paramMap);
if( null != propertyInfo ){
request.addProperty(propertyInfo);
}
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( SoapEnvelope.VER10 | SoapEnvelope.VER11 );
envelope.bodyOut = request;
envelope.dotNet = getDotNet();
envelope.setOutputSoapObject(request);
HttpTransportSE transport = new HttpTransportSE(serviceAction, getTimeout());
transport.debug = true;
transport.call( nameSpace + methodName , envelope);
SoapObject soapObject = (SoapObject) envelope.bodyIn;
return soapObject;
}
private PropertyInfo getPropertyInfo(Map<String, String> paramMap) throws Exception{
PropertyInfo propertyInfo = null;
if(null!=paramMap && paramMap.size()>0){
propertyInfo = new PropertyInfo();
for(Entry<String, String> entry : paramMap.entrySet()){
propertyInfo.setName(entry.getKey());
propertyInfo.setValue(entry.getValue());
propertyInfo.setType(String.class);
}
}
return propertyInfo;
}
public Boolean getDotNet() {
return dotNet;
}
/** 设置 .net提供的WebService */
public void setDotNet(Boolean dotNet) {
this.dotNet = dotNet;
}
/** 请求响应时间(毫秒) */
public Integer getTimeout() {
return timeout;
}
/** 设置请求响应时间(毫秒) */
public void setTimeout(Integer timeout) {
this.timeout = timeout;
}
}
/**
* 读取属性文件
* @author kewh 2012-07-24
*/
public class LoadProperties {
private static LoadProperties loadPropertie = null;
/** 获取实例 */
public static synchronized LoadProperties getInstance() {
if (loadPropertie == null) {
loadPropertie = new LoadProperties();
}
return loadPropertie;
}
private Properties getPropertie(){
Properties config = new Properties();
try {
InputStream inStream = getClass().getClassLoader().getResourceAsStream("resources/service_address.properties");
config.load(inStream);
} catch (Exception e) {
e.printStackTrace();
}
return config;
}
private Properties getPropertie(String address) {
Properties config = new Properties();
try {
InputStream inStream = getClass().getClassLoader().getResourceAsStream(address);
config.load(inStream);
} catch (Exception e) {
e.printStackTrace();
}
return config;
}
public String getPropertieValue(String key) {
String result = null;
Properties config = getInstance().getPropertie();
if (config != null) {
result = config.getProperty(key);
}
return result;
}
public String getPropertieValue(String address,String key) {
String result = null;
Properties config = getInstance().getPropertie(address);
if (config != null) {
result = config.getProperty(key);
}
return result;
}
}