1、首先確定對方開發WebService時使用的包
- axis1和axis2並不兼容
2、確定調用的方法
1、如對方給與方法使用該方法
2、使用axis的call方式
// 遠程調用路徑 ,無後綴wsdl
String endpoint = "http://IP地址:端口號/*/*/*";
String result = "call failed!";
//調用方法名
String mothod = "Test";
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new URL(endpoint));
//new QName的URL是要指向的命名空間的名稱,這個URL地址在你的wsdl打開後可以看到的,
//上面有寫着targetNamespace="http://*.*.*/",這個就是你的命名空間值了;
call.setOperationName(mothod); // 調用的方法名
// 設置參數名 :參數名 ,參數類型:String, 參數模式:'IN' or 'OUT'
call.addParameter(new QName("命名空間","param1"),XMLType.XSD_STRING,ParameterMode.IN);
call.addParameter(new QName("命名空間","param2"),XMLType.XSD_STRING,ParameterMode.IN);
call.addParameter(new QName("命名空間","param3"),XMLType.XSD_STRING,ParameterMode.IN);
call.setEncodingStyle("UTF-8");
String param1 = "";
String param2 = "";
String param3 = "";
call.setReturnType(Constants.XSD_STRING);
call.setUseSOAPAction(true);
try {
result = (String) call.invoke(new Object[] {param1,param2,param3});// 遠程調用
System.out.println(result);
JSONObject resultJson = JSONObject.fromObject(result);
String resultDataXML = resultJson.getString("data");
if( "".equals(resultDataXML) && resultDataXML != null ){
Boolean flag = RealResultXML(resultDataXML);
if(flag){
System.out.println("解析成功");
}else {
System.out.println("解析失敗");
}
}else{
System.out.println("無數據");
}
} catch (Exception e) {
System.out.println(result);
e.printStackTrace();
}
3、使用wsimport(jdk)方式(懶人專用)
打開jdk下的bin目錄 看下能否找到"wsimport.exe"這個文件
一般情況下都會有
如果沒有則說明你的JDK不支持這個功能
然後在DOS窗口下輸入wsimport 敲回車
如果提示錯誤的話 說明你的JDK環境變量還沒有配好
如果OK的話 輸入以下命令就可以將wsdl文件生成java文件了
wsimport http://172.16.7.107:9090/com.kutesmart.esb.ldap.sso?wsdl -keep -p com.llg.ws2 -s g:/ws
參數說明
http://172.16.7.107:9090/com.kutesmart.esb.ldap.sso?wsdl wsdl文件url
-keep 是否生成源文件
-p com.llg.ws2 生成後的java包名
-s g:/ws 生成後放哪個目錄
注意:wsimport與xfire衝突
原因:創建 Service 的代理的時候沒有使用 jdk 自帶的 javax.xml.ws.spi.Provider,而是使用了 xfire 包中的 javax.xml.ws.spi.Provider
處理方法:把 xfire 包中的 jaxws 子包刪除掉就可以了
4、鏈接直接獲取數據(直接把系統方法暴露)
//第一步:創建服務地址
URL url = new URL("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl");
//第二步:打開一個通向服務地址的連接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//第三步:設置參數
//3.1發送方式設置:POST必須大寫
connection.setRequestMethod("POST");
//3.2設置數據格式:content-type
connection.setRequestProperty("content-type", "text/xml;charset=utf-8");
//3.3設置輸入輸出,因爲默認新創建的connection沒有讀寫權限,
connection.setDoInput(true);
connection.setDoOutput(true);
//第四步:組織SOAP數據,發送請求
String soapXML = getXML("17321242779");
//將信息以流的方式發送出去
OutputStream os = connection.getOutputStream();
os.write(soapXML.getBytes());
//第五步:接收服務端響應,打印
int responseCode = connection.getResponseCode();
if(200 == responseCode){//表示服務端響應成功
//獲取當前連接請求返回的數據流
InputStream is = connection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String temp = null;
while(null != (temp = br.readLine())){
sb.append(temp);
}
/**
* 打印讀取結果
*/
System.out.println(sb.toString());
is.close();
isr.close();
br.close();
}
os.close();