关于webservice客户端

package com.becom.account.webapp.webservice;


import java.io.BufferedReader;  
import java.io.BufferedWriter;  
import java.io.File;  
import java.io.FileInputStream;  
import java.io.FileOutputStream;
import java.io.InputStream;  
import java.io.InputStreamReader;  
import java.io.OutputStream;  
import java.io.OutputStreamWriter;     
import java.math.BigDecimal;
import java.net.URL;  
import java.net.URLConnection;   
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import com.becom.account.model.Net;


public class GetNetAccount {

 /*public static void main(String[]args) throws Exception{
  GetNetAccount getNetAccount=new GetNetAccount();
  List<Net> nets=getNetAccount.getUsers();
  for(Net net:nets){
   System.out.println("---"+net.getUniqueId().trim()+"---");
  }
  
  
 }*/
 public  List<Net> getUsers() throws Exception{
  
  FileOutputStream ops=new FileOutputStream("user.xml");
  Net net=new Net();
  List<Net> nets=new ArrayList<Net>();
       try{
       
        InputStream is = getSoapInputStream();  // 获取服务器端返回的流  
      BufferedReader br = new BufferedReader(new InputStreamReader(is,"UTF-8")); //使用UTF-8进行编码
         String line="";  
          //取到东西写向文件  
          //ops=new FileOutputStream("user.xml");
         
          //BufferedWriter bw=new BufferedWriter(w);  
         while((line=br.readLine())!=null){
          System.out.println(line);
          byte[] bs=line.getBytes();
          ops.write(bs);
         }
       
         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();  
      DocumentBuilder db = dbf.newDocumentBuilder();  
      Document doc = db.parse("user.xml");
      NodeList nl = doc.getElementsByTagName("user_struct");
    
     
      for (int i=0;i<nl.getLength();i++){
       String str=doc.getElementsByTagName("uniqueid").item(i).getFirstChild().getNodeValue().trim();
       net.setUserId(new BigDecimal(str));
             System.out.println("用户编号:" +

             doc.getElementsByTagName("uid").item(i).getFirstChild().getNodeValue());
            
             net.setUserName(doc.getElementsByTagName("username").item(i).getFirstChild().getNodeValue());
             System.out.println("用户名:" +

             doc.getElementsByTagName("username").item(i).getFirstChild().getNodeValue());
            
             net.setLinkEmail(doc.getElementsByTagName("email").item(i).getFirstChild().getNodeValue());
             System.out.println("email:" +

               doc.getElementsByTagName("email").item(i).getFirstChild().getNodeValue());
            
             net.setLinkMobile(doc.getElementsByTagName("phone").item(i).getFirstChild().getNodeValue());
             System.out.println("手机号:" +

               doc.getElementsByTagName("phone").item(i).getFirstChild().getNodeValue());
            
             net.setUniqueId(str);
             System.out.println("用户唯一标识:" +

               doc.getElementsByTagName("uniqueid").item(i).getFirstChild().getNodeValue());
            
            
             //OutputStream a=new FileOutputStream("user.xml",false);
             //BufferedOutputStream out=new BufferedOutputStream(a);
             //out.write('Y');
       //out.flush();
             nets.add(net);
             }
        }catch(Exception e){
        e.printStackTrace();
        }finally{
        if(ops!=null){
         ops.close();
        }
        File file=new File("user.xml");
        file.delete();
       }
 
           /*DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
           factory.setNamespaceAware(true);
           DocumentBuilder builder=factory.newDocumentBuilder();
          
           Document doc = builder.parse(is);
          

           NodeList nl = doc.getElementsByTagName("user_struct");*/
     
           // temp 就是返回的XML文件  
           /*下面可以使用DOM来解析当前返回的XML*/ 
            //DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();  
           // DocumentBuilder db = dbf.newDocumentBuilder();  
            //Document doc = db.parse(is); 
           return nets; 
             //return user;
       }  
    
       /** 
        * 读取请求文件  
        * @return 
        * @throws Exception 
        */ 
       public static String getResource() throws Exception {
     
        File file=new File("xxx");//以xml格式保存的请求头文件
        
           InputStream is = new FileInputStream(file);
           BufferedReader br = new BufferedReader(new InputStreamReader(is));  //创建文件输入流  
           StringBuffer sb = new StringBuffer();    
           String temp=br.readLine();  
           while ((temp = br.readLine()) != null) {  
               sb.append(temp);    // 将文件的内容读取进来  
           }  
           br.close();  
           String soap = sb.toString();  
           //soap = soap.replace("{city}", city);    
           //soap = soap.replace("{cityId}", cityId); // 将请求文件中的占位符替换成我们所要传递的参数  
           return soap;  
       }  
    
       /** 
        * 该方法得到服务器端返回的输入流 
        * @return 
        * @throws Exception 
        */ 
       private static InputStream getSoapInputStream()  
               throws Exception {  
           String soap = getResource(); 
           if (soap != null) {  
              URL url = new URL(  
                       "xxx");//访问的URL地址       
               /*URLConnection uc = url.openConnection(new Proxy(Type.HTTP,  
                       new InetSocketAddress("192.168.0.1", 808))); */ // 建立连接,该处使用代理服务器.  
               //如果没使用代理服务器的话, 则直接使用url.openConnection();  
               URLConnection uc=url.openConnection();
               uc.setUseCaches(false); // 设置不缓存  
               uc.setDoInput(true);    // 设置可以输出  
               uc.setDoOutput(true);   // 设置可以输入   
    
               /**  
                 * POST /ws/siteuser.asmx HTTP/1.1 
                 * Host: www.bdstarlbs.com 
                 * Content-Type: application/soap+xml; charset=utf-8 
                 * Content-Length: length 
                 * 下面设置头文件中的信息,可以将上面请求文件中的头信息设置进去 
                 * 其中的length = 文件内容的长度 
                 */ 
               uc.setRequestProperty("Content-Type","application/soap+xml; charset=utf-8");
               uc.setRequestProperty("Content-Length", String.valueOf(soap.length()));  
               OutputStream os = uc.getOutputStream();   //获取连接的输入流  
               BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os,  
                       "UTF-8"));  
   
               bw.write(soap);  // 将请求的内容发送到服务器  
               bw.flush();  
               bw.close();  
                 
               InputStream is = uc.getInputStream();  //  获取服务器返回的流  
               return is;  
           }  
           return null;  

       }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值