1. 程式人生 > >Axis2 java呼叫.net webservice介面的問題(鄭州就維)

Axis2 java呼叫.net webservice介面的問題(鄭州就維)

這是一個古老的問題,古老到從我若干年前遇到這樣的問題就是一個解決之道:反覆嘗試。其實標準是什麼,標準就是一個束縛,一種按既定規則的束縛,錯點點,你的呼叫就可能不成功,不成功後你要花費大量的力氣查詢原因和錯誤,差異很多帖子,查詢相似的地方,Webservice的實現不同,Soap,CXF,Axis等,每種工具都有指定的方式,剛開始嘗試Soap發現這個根本沒合適的包進行呼叫,也是IBM比較老的jar,2001年寫的比較複雜,我對比較複雜的東西向來不感興趣,因為太複雜我也搞不懂。索性用Axis2,在呼叫之前你要知道soap的兩個協議版本1.1,1.2是不太一樣的。

問題一:org.apache.axis2.AxisFault: 伺服器未能識別 HTTP 頭 SOAPAction 的值

這個錯誤錯誤查了好久,最後發現對方給的資料裡面忘記給名稱空間地址了,我去真是頭疼,注意的是需要加方法名稱

http://tempuri.org/GetSign

問題二:org.apache.axis2.AxisFault: 伺服器無法處理請求。 ---> 未將物件引用設定到物件的例項。

這個錯誤,也是滿天飛的帖子其實是一個很小的問題,反斜槓

OMNamespace omNs = fac.createOMNamespace("http://tempuri.org/", "");

標準吧,只要錯那麼點點,你就別想調通了,我把整個程式碼示例貼到下面:
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.transport.http.HTTPConstants;


public class SoapAxis {
     private static EndpointReference targetEPR = new EndpointReference("http://192.168.0.185/OnlinePaywebservice/platformws.asmx");
	public static void main(String[] args) {
        Options options = new Options();
        options.setAction("http://tempuri.org/GetSign");// 呼叫介面方法
        options.setTo(targetEPR);
        options.setProperty(HTTPConstants.CHUNKED, "false");//設定不受限制.


        ServiceClient sender = null;
        try {
             sender = new ServiceClient();
             sender.setOptions(options);
             OMFactory fac = OMAbstractFactory.getOMFactory();
             OMNamespace omNs = fac.createOMNamespace("http://tempuri.org/", "");
             OMElement method = fac.createOMElement("GetSign", omNs);
             OMElement name = fac.createOMElement("prestr", omNs);// 設定入參名稱
             OMElement name2 = fac.createOMElement("key", omNs);// 設定入參名稱
             name.setText("hawei");// 設定入參值
             name2.setText("6181a1fb89564b589283ad578baa7d5e");
             method.addChild(name);
             method.addChild(name2);
             method.build();
             System.out.println("method:" + method.toString());
             OMElement response = sender.sendReceive(method);
             System.out.println("response:" + response);
             OMElement elementReturn = response.getFirstElement();
             System.out.println("cityCode:" + elementReturn.getText());
          } catch (AxisFault e) {
             System.out.println("Error");
               e.printStackTrace();
          }
       }
}

問題三:org.apache.axis2.AxisFault: 伺服器無法讀取請求。 ---> XML 文件(1, 291)中有錯誤。 ---> 字串“2014-12-09 02:03:00”不是有效的 AllXsd 值。

這個問題主要是日期格式問題,日期格式改成“2014-12-09T02:03:00

		String timestamp1 =new SimpleDateFormat("yyyy-MM-dd").format(nowDate);
		String timestamp2 =new SimpleDateFormat("hh:mm:ss").format(nowDate);
		String timestampString=timestamp1+" "+timestamp2;

問題四:SOAP頭如何加
        public static void addValidation(ServiceClient serviceClient) {
		OMFactory fac = OMAbstractFactory.getOMFactory();
		OMNamespace omNs = fac.createOMNamespace(tns, "");
		OMElement header = fac.createOMElement("CredentialSoapHeader", omNs);
		OMElement appId = fac.createOMElement("AppID", omNs);
		//
		appId.setText("145");
		header.addChild(appId);
		System.out.println("header:" + header.toString());
		serviceClient.addHeader(header);
	}


下面羅列下需要的Axis2的包,包實在太多,不要都打入進來