import java.net.URL;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
import java.util.Vector;
import org.apache.soap.Constants;
import org.apache.soap.Fault;
import org.apache.soap.rpc.Call;
import org.apache.soap.rpc.Parameter;
import org.apache.soap.rpc.Response;
import utlis.JDBCUtil;
public class HelloSOAPClient
{
public static void main(String[] args) throws Exception
{
System.out.println("SOAP调用测试开始。。。");
///System.out.println("输入你要输入的网址:");
//接收键盘的输入
///Scanner sc =new Scanner(System.in);
///int i =sc.nextInt();
String name = "jack";
String name1="swallow1";
///String urlName=sc.nextLine();
HelloSOAPClient helloSOAPClient = new HelloSOAPClient();
//调用远程的SOAP服务///
//String resultValue = helloSOAPClient.doSOAPRequest("hello.getWelcomeValue","getWelcomeValue","name",name);
//String resultValue = helloSOAPClient.doSOAPRequest("hello.getIdleChair","getIdleChair","name",name);
String resultValue = helloSOAPClient.doSOAPRequest1("hello.add","add","call",name,"callPhone",name1);
//String resultValue = helloSOAPClient.doSOAPRequest("hello.setChairIdle","setChairIdle","name",name);
//String resultValue = helloSOAPClient.doSOAPRequest("hello.setChairBusy","setChairBusy","name",name);
System.out.println("urlName:" + name);
System.out.println("Result:" + resultValue);
System.out.println("SOAP调用测试结束。。。");
}
public String doSOAPRequest(String _strURI,String _strMethodName,String _strName,String _strValue) throws Exception
{
//创建一个远程调用
Call call = new Call();
//设置远程对象的URI
call.setTargetObjectURI("urn:" + _strURI);
//设置调用的方法名
call.setMethodName(_strMethodName);
//设置编码风格
call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
//设置方法调用的参数
Vector params = new Vector();
params.addElement(new Parameter(_strName,String.class,_strValue,null));
call.setParams(params);
//发送RPC请求,请查看webapps\soap\web.xml,就知道rpcrouter是映射到org.apache.soap.server.http.RPCRouterServlet这个类
//for mas http://localhost:80/soap urn:maswsappapi
Response resp = call.invoke(new URL("http://58.210.206.240:9999/soap/servlet/rpcrouter"),"");
if(resp.generatedFault())
{
//远程调用出错处理
Fault fault = resp.getFault();
System.out.println("调用失败!");
System.out.println("错误代号:" + fault.getFaultCode());
System.out.println("错误信息:" + fault.getFaultString());
return "调用失败!";
}
else
{
//调用成功,获取返回值
Parameter result = resp.getReturnValue();
return null;
//return result.getValue().toString();
}
}
public String doSOAPRequest1(String _strURI,String _strMethodName,String _strName1,String _strValue1,String _strName2,String _strValue2) throws Exception
{
//创建一个远程调用
Call call = new Call();
//设置远程对象的URI
call.setTargetObjectURI("urn:" + _strURI);
//设置调用的方法名
call.setMethodName(_strMethodName);
//设置编码风格
call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
//设置方法调用的参数
Vector params = new Vector();
params.addElement(new Parameter(_strName1,String.class,_strValue1,null));
params.addElement(new Parameter(_strName2,String.class,_strValue2,null));
call.setParams(params);
//发送RPC请求,请查看webapps\soap\web.xml,就知道rpcrouter是映射到org.apache.soap.server.http.RPCRouterServlet这个类
//for mas http://localhost:80/soap urn:maswsappapi
Response resp = call.invoke(new URL("http://58.210.206.240:9999/soap/servlet/rpcrouter"),"");
if(resp.generatedFault())
{
//远程调用出错处理
Fault fault = resp.getFault();
System.out.println("调用失败!");
System.out.println("错误代号:" + fault.getFaultCode());
System.out.println("错误信息:" + fault.getFaultString());
return "调用失败!";
}
else
{
//调用成功,获取返回值
Parameter result = resp.getReturnValue();
return null;
//return result.getValue().toString();
}
}
}