反正因為某些ooxx的原因~
要去叫.net的webservice..
但是因為axis在製定內容的方式不一樣~所以要改寫一下..
部分內容牽涉實體位置~ 用xxx表示
.net的xml參數~ 似乎不需要排序~ 目前用Map也能被對方接受~
幾個要點CHUNK=false
rpcs.getOptions().setProperty(org.apache.axis2.transport.http.HTTPConstants.CHUNKED, Boolean.FALSE);
另外.net常用xmlns..這個屬性於axis內必須用add atrribute的方式寫入, QName會綁某些東西會建不出來...
OMAttribute attr = fac.createOMAttribute("xmlns", null, "http://tempuri.org/");
element.addAttribute(attr);
import org.apache.axiom.om.*;
import org.apache.axiom.om.util.AXIOMUtil;
import org.apache.axis2.client.*;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.rpc.client.RPCServiceClient;
import javax.xml.namespace.QName;
import java.util.*;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.httpclient.*;
public class Axis2Client {
String PROPERTY_KEY="xxxxx";
OMFactory fac=OMAbstractFactory.getOMFactory();
RPCServiceClient rpcs=null;
EndpointReference er=null;
/**constructor
* @param serverURL set ws server URL*/
public Axis2Client(String serverURL) {
if(serverURL == null) {
serverURL = Property.getBundle().getString(PROPERTY_KEY);
}
try {
er = new EndpointReference(serverURL);
fac=OMAbstractFactory.getOMFactory();
rpcs = new RPCServiceClient();
} catch(Exception e) {
Log.logError(this, "create rpc client error", e);
}
}
/**get .NET webservice (Major) and return OMElement Object of response
* @param serverURL
* @param op
* @param params
* @return String*/
public OMElement getResult(String op, Map params) throws Exception {
rpcs.getOptions().setProperty(org.apache.axis2.transport.http.HTTPConstants.CHUNKED, Boolean.FALSE);
rpcs.getOptions().setAction("http://tempuri.org/"+op);
rpcs.getOptions().setTo(er);
String xml = "<"+op+">";
if(params != null) {
Object[] keys = params.keySet().toArray();
for(int i = 0 ; i < keys.length ; i++) {
if(params.get(keys[i]) != null) {
String tag = (String)keys[i];
String val = (String)params.get(keys[i]);
xml += "<"+tag+">"+val+""+tag+">";
}
}
}
xml += ""+op+">";
//System.out.println("??="+xml);
OMElement element = AXIOMUtil.stringToOM(xml);
OMAttribute attr = fac.createOMAttribute("xmlns", null, "http://tempuri.org/");
element.addAttribute(attr);
System.out.println("??method="+element);
OMElement result= rpcs.sendReceive(element);
return result;
}
/**get .NET webservice (Major) and return xml string of response
* @param serverURL
* @param op
* @param params
* @return String*/
public String getResultXML(String op, Map params) throws Exception {
OMElement result = getResult(op, params);
String re = StringEscapeUtils.unescapeXml(result.toString());
return re;
}
/***/
protected void finalize() {
try {
rpcs.cleanup();
rpcs.cleanupTransport();
} catch(Exception e) {}
}
/***/
public void endConnection() {
try {
rpcs.cleanup();
rpcs.cleanupTransport();
} catch(Exception e) {}
}
public static void main(String[] agvs) {
//proxy if need
Properties p = System.getProperties();
p.put("http.proxyHost", "xxx");
p.put("http.proxyPort", "xxx");
String ul = "http://xxx/xxx/WebService/xxx.asmx?WSDL";
Axis2Client ac = new Axis2Client(ul);
String op = "HelloWorld";
Map mp = new HashMap();
mp.put("xx", "xxx");
mp.put("xxx", "xxx");
try {
System.out.println(ac.getResultXML(op, mp));
} catch(Exception e) {
e.printStackTrace();
} finally {
ac.endConnection();
}
}
}
沒有留言:
張貼留言