package net.logcd.ws.axis.test;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.apache.axis.types.Schema;
import org.junit.Test;
public class TestClientAxisWS {
@Test
public void invokeHelloService_WS() throws Exception {
String endPointUrl = "http://127.0.0.1/axis/helloService";
String namespaceURI = "http://net.logcd.axis";
Service service = new Service();
Call call = (Call) service.createCall();
call.addParameter("userName", XMLType.XSD_STRING, ParameterMode.IN);
call.setReturnType(XMLType.XSD_STRING);
URL url = new URL(endPointUrl);
call.setTargetEndpointAddress(url);
QName opName = new QName(namespaceURI, "sayHello");
call.setOperationName(opName);
Object retObj = call.invoke(new Object[] { "lucy" });
System.out.println(retObj.toString());
}
/**
* 取天气预报信息(.net开发的WebService)
* @throws Exception
*/
@Test
public void invokeWeatherService() throws Exception {
String endPointUrl = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl";
String namespaceURI = "http://WebXml.com.cn/";
String soapActionURI = "http://WebXml.com.cn/getWeatherbyCityName";
String method = "getWeatherbyCityName";
Service service = new Service();
Call call = (Call) service.createCall();
call.addParameter(new QName(namespaceURI, "theCityName"), XMLType.XSD_STRING, ParameterMode.IN);
call.setReturnType(XMLType.XSD_SCHEMA);
call.setTargetEndpointAddress(new URL(endPointUrl));
call.setUseSOAPAction(true);
call.setSOAPActionURI(soapActionURI);
call.setOperationName(new QName(namespaceURI, method));
Schema result = (Schema) call.invoke(new Object[] { "成都" });
StringBuffer buff = new StringBuffer();
for (int i = 0; i < result.get_any().length; i++) {
buff.append(result.get_any()[i]);
}
//把多替换成\u591A
System.out.println(unicodeToString(buff.toString().replaceAll("&#x(.*?);","\\\\u$1")));
}
public static String unicodeToString(String str) {
Charset set = Charset.forName("UTF-16");
Pattern p = Pattern.compile("\\\\u([0-9a-fA-F]{4})");
Matcher m = p.matcher(str);
int start = 0;
int start2 = 0;
StringBuffer sb = new StringBuffer();
while (m.find(start)) {
start2 = m.start();
if (start2 > start) {
String seg = str.substring(start, start2);
sb.append(seg);
}
String code = m.group(1);
int i = Integer.valueOf(code, 16);
byte[] bb = new byte[4];
bb[0] = (byte) ((i >> 8) & 0xFF);
bb[1] = (byte) (i & 0xFF);
ByteBuffer b = ByteBuffer.wrap(bb);
sb.append(String.valueOf(set.decode(b)).trim());
start = m.end();
}
start2 = str.length();
if (start2 > start) {
String seg = str.substring(start, start2);
sb.append(seg);
}
return sb.toString();
}
}