81,092
社区成员
发帖
与我相关
我的任务
分享
import javax.xml.namespace.QName;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
public class CallWsdl {
public static void main(String[] args) {
try {
// 使用RPC方式调用WebService
RPCServiceClient serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
// 指定调用WebService的URL
EndpointReference targetEPR = new EndpointReference(
"http://localhost:8080/tiger/wstiger/TraderService?wsdl");
options.setTo(targetEPR);
// 指定sayHelloToPerson方法的参数值
Object[] opAddEntryArgs = new Object[] {"2c90817157ffd27f0157ffd9943a0171"};
// 指定sayHelloToPerson方法返回值的数据类型的Class对象
Class[] classes = new Class[] {String.class};
// 指定要调用的sayHelloToPerson方法及WSDL文件的命名空间
QName opAddEntry = new QName("http://service.ws.tiger.com", "getTraderInfo");
// 调用sayHelloToPerson方法并输出该方法的返回值
System.out.println(serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs, classes)[0]);
} catch (Exception e) {
e.printStackTrace();
}
}
}
@WebService(targetNamespace="http://service.ws.tiger.com")
public interface TraderService {
public String getTraderInfo(@WebParam(name="usercode",targetNamespace = "http://service.ws.tiger.com") String usercode) throws Exception;
}
URL wsUrl = new URL("http://localhost:8080/tiger/wstiger/TraderService?wsdl");
HttpURLConnection conn = (HttpURLConnection) wsUrl.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
OutputStream os = conn.getOutputStream();
//请求体
String soap = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" +
"<soapenv:Body> <getTraderInfo xmlns=\"http://service.ws.tiger.com\"><usercode>2c90817157ffd27f0157ffd9943a0171</usercode> </getTraderInfo> </soapenv:Body> </soapenv:Envelope>";
os.write(soap.getBytes());
InputStream is = conn.getInputStream();
byte[] b = new byte[1024];
int len = 0;
String s = "";
while((len = is.read(b)) != -1){
String ss = new String(b,0,len,"UTF-8");
s += ss;
}
System.out.println(s);
is.close();
os.close();
conn.disconnect();