JAVA呼叫webservice服務的兩種方法
一、直接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;
}