一、直接AXIS调用远程的web service
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import javax.xml.namespace.QName;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
//private String url="http://www.webxml.com.cn/WebServices/WeatherWebService.asmx";//提供接口的地址
//private String soapaction="http://WebXml.com.cn/"; //域名,这是在server定义的
public static String getResult(String methodName,Map<String,Object> map){
Service service=new Service();
String url = Constants.serveUrl;//提供接口的地址
String soapaction = Constants.soapaction;//域名,这是在server定义的(具体见提供的asmx)
String v= "";
try{
Call call=(Call)service.createCall();
call.setTargetEndpointAddress(url);
call.setOperationName(new QName(soapaction,methodName)); //设置要调用哪个方法
Set set = map.entrySet();
List<Object> list = new ArrayList<Object>();
for(Iterator iter = set.iterator(); iter.hasNext();){
Map.Entry entry = (Map.Entry)iter.next();
String key = (String)entry.getKey();
String value = (String)entry.getValue();
if(key.equals("userId") || key.equals("genSetId")){//判断参数,设置参数类型
call.addParameter(new QName(soapaction,key), //设置要传递的参数
org.apache.axis.encoding.XMLType.XSD_INT,javax.xml.rpc.ParameterMode.IN);
}else{
call.addParameter(new QName(soapaction,key), //设置要传递的参数
org.apache.axis.encoding.XMLType.XSD_STRING,javax.xml.rpc.ParameterMode.IN);
}
list.add(value);
}
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);//(标准的类型)
call.setUseSOAPAction(true);
call.setSOAPActionURI(soapaction+methodName);
v=(String)call.invoke(list.toArray());//调用方法并传递参数
System.out.println("result is "+v);
}catch(Exception ex){
ex.printStackTrace();
v = "fail";
}
return v;
}
2、 HTTP POST 请求
public static String getWebSerResult(String methodname,Map<String,Object> map){
String result = "";
try {
String urlStr = Constants.serveUrl;//提供接口的地址
String soapaction = Constants.soapaction;//域名
URL url = new URL(urlStr);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setDoInput(true);
con.setDoOutput(true);
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
con.setConnectTimeout(30000);
con.setReadTimeout(30000);
con.connect();
OutputStream oStream = con.getOutputStream();
//下面这行代码是用字符串拼出要发送的xml,xml的内容是从测试软件里拷贝出来的
//需要注意的是,有些空格不要弄丢哦,要不然会报500错误的。
//参数什么的,你可以封装一下方法,自动生成对应的xml脚本
String soap = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"+
"<soap:Envelope "+
"xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" "+ "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "+ "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">"+
"<soap:Body>"+
"<SaveSetDateTimeCmd xmlns=\""+soapaction+"\">"+
"<userId>1</userId>"+
"<ipAddress>170.0.0.1</ipAddress>"+
"<genSetId>1</genSetId>"+
"<cmdCode>11</cmdCode>"+
"<newDateTime>2018-05-28 10:54:00</newDateTime>"+
"</SaveSetDateTimeCmd > "+
"</soap:Body>"+
"</soap:Envelope>";
oStream.write(soap.getBytes());
oStream.flush();
oStream.close();
InputStream iStream = con.getInputStream();
Reader reader = new InputStreamReader(iStream);
int tempChar;
String str = new String();
while((tempChar = reader.read()) != -1){
str += (char) tempChar;
}
iStream.close();
oStream.close();
con.disconnect();
System.out.println(">>>>>>returnedxmlstr:"+str);
Map<String,Object> remap = parseXmlToList(str);
System.out.println("**********remap**********:"+remap);
result = (String) remap.get("Body");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
result = "fail";
}
return result;
}