Java调用 .netWebService调用

 Java使用axis来调用.net写的asmx WebService,大体步骤如下。

Axis需要用到的包如下:axis.jar、commons-discovery-0.2.jar、commons-logging-1.0.4.jar、jaxrpc.jar、wsdl4j-1.5.1.jar

java调用的代码如下:

   String wsdlUrl = http://ipaddress/test/Service1.asmx;  
   String nameSpaces = "http://tempuri.org/"; //注意最后的/

 

   //设置连接主机的超时时间 ms

   System.setProperty("sun.net.client.defaultConnectTimeout","300000");

   //设置从主机读取数据的超时时间

   System.setProperty("sun.net.client.defaultReadTimeout","300000"); 


   Service service = new Service();  
   Call call = (Call) service.createCall();

   call.setOperationName(new QName("http://tempuri.org/","InsertString"));
   call.setTargetEndpointAddress(new java.net.URL(wsdlUrl));
   
   call.addParameter(new QName(nameSpaces,"UserName"), org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
   call.addParameter(new QName(nameSpaces,"Password"), org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
   call.addParameter(new QName(nameSpaces,"ID"), org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
   call.addParameter(new QName(nameSpaces,"ORDER_NO"), org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
   call.addParameter(new QName(nameSpaces,"ORDER_TYPE"), org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
   call.addParameter(new QName(nameSpaces,"ORDER_STATUS"), org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
   call.addParameter(new QName(nameSpaces,"USER_ID"), org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
   ................................

   call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);
   call.setUseSOAPAction(true);
   call.setSOAPActionURI(nameSpaces + "InsertString");

   
   Object[] objs = new Object[]{"******", "********", order.getId(), order.getOrderNo(), order.getOrderType(), order.getOrderStatus(), order.getUserId(),
     order.getDomainId(), order.getRemittanceId(), order.getPrice(), order.getTerm(), order.getOrderContact(), order.getOrderPhone(),
     order.getOrderContent(), order.getOrderResult(), order.getCreateDate(), order.getCreateUser(), order.getUpdateDate(), order.getUpdateUser()};
   
   Object res = call.invoke( objs );
   System.out.println(res);
  }
  catch (Exception ex) {            
   System.err.println(ex.toString());
  }

 

在Java中调用.NET Web Services,处理返回的数据集合
       try ...{

            String wsdlUrl = "http://localhost/WebSite1/Service.asmx?op=GetPersonTable";  
            String soapActionURI = "http://tempuri.org/GetPersonTable";  

            Service service = new Service();  
            Call call = (Call) service.createCall();   
            // 
            call.setOperationName(new QName("http://tempuri.org/","GetPersonTable"));
            call.setTargetEndpointAddress(new java.net.URL(wsdlUrl));
            call.addParameter("a", org.apache.axis.encoding.XMLType.XSD_STRING,  
                    javax.xml.rpc.ParameterMode.IN);
            call.setReturnType(org.apache.axis.encoding.XMLType.XSD_SCHEMA);  
            call.setUseSOAPAction(true);  
            call.setSOAPActionURI(soapActionURI);
           
            Object[] objs = new Object[]...{"ssss"};
            Object res = call.invoke( objs );
            System.out.println(res);
            Schema schema = (Schema)res;
                    
            DefaultTableModel model=new  DefaultTableModel(new String[]...{"name","gender"},0);
            schema.get_any()[1].getChildNodes().getLength();
            int nLength=schema.get_any()[1].getChildNodes().item(0).getChildNodes().getLength();
            String name="N/A";
            String gender="N/A";
            for(int i=0;i<nLength;i++)
            ...{
                if(schema.get_any()[1].getChildNodes().item(0).getChildNodes().item(i).getChildNodes().item(0).getNodeName().equals("Name"))
                ...{
                    name=schema.get_any()[1].getChildNodes().item(0).getChildNodes().item(i).getChildNodes().item(0).getFirstChild().getNodeValue();
                }
                if(schema.get_any()[1].getChildNodes().item(0).getChildNodes().item(i).getChildNodes().item(1).getNodeName().equals("Gender"))
                ...{
                    gender=schema.get_any()[1].getChildNodes().item(0).getChildNodes().item(i).getChildNodes().item(1).getFirstChild().getNodeValue();
                }   
                model.addRow(new String[]...{name,gender});
                this.jScrollPane1.getViewport().add(jTable1, null);
            }
            jTable1.setModel(model);       
      }
      catch (Exception ex)
      ...{            
          System.err.println(ex.toString());
      }  

 

 

Java中可以使用HttpURLConnection来请求WEB资源。
HttpURLConnection对象不能直接构造,需要通过URL.openConnection()来获得HttpURLConnection对象,示例代码如下:
String urlStr= www.app.org;
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();

HttpURLConnection是基于HTTP协议的,其底层通过socket通信实现。如果不设置超时(timeout),在网络异常的情况下,可能会导致程序僵死而不继续往下执行。可以通过以下两个语句来设置相应的超时:
System.setProperty("sun.net.client.defaultConnectTimeout", 超时毫秒数字符串);
System.setProperty("sun.net.client.defaultReadTimeout", 超时毫秒数字符串);

其中: sun.net.client.defaultConnectTimeout:连接主机的超时时间(单位:毫秒)
sun.net.client.defaultReadTimeout:从主机读取数据的超时时间(单位:毫秒)

例如:
System.setProperty("sun.net.client.defaultConnectTimeout", "30000");
System.setProperty("sun.net.client.defaultReadTimeout", "30000");

JDK 1.5以前的版本,只能通过设置这两个系统属性来控制网络超时。在1.5中,还可以使用HttpURLConnection的父类URLConnection的以下两个方法:
setConnectTimeout:设置连接主机超时(单位:毫秒)
setReadTimeout:设置从主机读取数据超时(单位:毫秒)

例如:
HttpURLConnection urlCon = (HttpURLConnection)url.openConnection();
urlCon.setConnectTimeout(30000);
urlCon.setReadTimeout(30000);

 

你可能感兴趣的:(java,String,object,schema,service,NameSpaces)