CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  Java >  Web Services / XML

不用AXIS也不用MSSoap如何通过Java调用远程的Web Service呢?

楼主ducadi83((大饼))2006-03-14 08:04:01 在 Java / Web Services / XML 提问

AXIS和MSSoap都要另外安装一些东西。  
  而我在异构系统中不能引入这么多东西,只能通过简单的Java程序来实现  
  理论上面讲只要按照web   service要求的格式传递相应的XML过去就可以了。请高手指点。  
  我这里在网上看到一个代码,但是总是会出现返回XML的时候的IOException。就是没有得到返回值。我查看了一下Web   service也没有得到执行。这是怎么回事呢?有没有更好的方法呢?  
  /**    
    *   SOAPClient4XG.   Read   the   SOAP   envelope   file   passed   as   the   second  
    *   parameter,   pass   it   to   the   SOAP   endpoint   passed   as   the   first   parameter,   and  
    *   print   out   the   SOAP   envelope   passed   as   a   response.     with   help   from   Michael  
    *   Brennan   03/09/01  
    *    
    *  
    *   @author     Bob   DuCharme  
    *   @version   1.1  
    *   @param       SOAPUrl             URL   of   SOAP   Endpoint   to   send   request.  
    *   @param       xmlFile2Send   A   file   with   an   XML   document   of   the   request.      
    *  
    *   5/23/01   revision:   SOAPAction   added  
  */  
   
  import   java.io.*;  
  import   java.net.*;  
   
  public   class   SOAPClient4XG   {  
          public   static   void   main(String[]   args)   throws   Exception   {  
  /*  
                  if   (args.length     <   2)   {  
                          System.err.println("Usage:     java   SOAPClient4XG   "   +  
                                                                "http://soapURL   soapEnvelopefile.xml"   +  
                                                                "   [SOAPAction]");  
  System.err.println("SOAPAction   is   optional.");  
                          System.exit(1);  
                  }  
  */  
   
                  String   SOAPUrl             =   "http://services.xmethods.net:80/soap/servlet/rpcrouter";  
                  String   xmlFile2Send   =   "weatherreq.xml";  
   
      String   SOAPAction   =   "";  
                  if   (args.length     >   2)    
  SOAPAction   =   args[2];  
   
                  //   Create   the   connection   where   we're   going   to   send   the   file.  
                  URL   url   =   new   URL(SOAPUrl);  
                  URLConnection   connection   =   url.openConnection();  
                  HttpURLConnection   httpConn   =   (HttpURLConnection)   connection;  
   
                  //   Open   the   input   file.   After   we   copy   it   to   a   byte   array,   we   can   see  
                  //   how   big   it   is   so   that   we   can   set   the   HTTP   Cotent-Length  
                  //   property.   (See   complete   e-mail   below   for   more   on   this.)  
   
                  FileInputStream   fin   =   new   FileInputStream(xmlFile2Send);  
   
                  ByteArrayOutputStream   bout   =   new   ByteArrayOutputStream();  
           
                  //   Copy   the   SOAP   file   to   the   open   connection.  
                  copy(fin,bout);  
                  fin.close();  
   
                  byte[]   b   =   bout.toByteArray();  
   
                  //   Set   the   appropriate   HTTP   parameters.  
                  httpConn.setRequestProperty(   "Content-Length",  
                                                                            String.valueOf(   b.length   )   );  
                  httpConn.setRequestProperty("Content-Type","text/xml;   charset=utf-8");  
      httpConn.setRequestProperty("SOAPAction",SOAPAction);  
                  httpConn.setRequestMethod(   "POST"   );  
                  httpConn.setDoOutput(true);  
                  httpConn.setDoInput(true);  
   
                  //   Everything's   set   up;   send   the   XML   that   was   read   in   to   b.  
                  OutputStream   out   =   httpConn.getOutputStream();  
                  out.write(   b   );          
                  out.close();  
   
                  //   Read   the   response   and   write   it   to   standard   out.  
   
                  InputStreamReader   isr   =  
                          new   InputStreamReader(httpConn.getInputStream());  
                  BufferedReader   in   =   new   BufferedReader(isr);  
   
                  String   inputLine;  
   
                  while   ((inputLine   =   in.readLine())   !=   null)  
                          System.out.println(inputLine);  
   
                  in.close();  
          }  
   
      //   copy   method   from   From   E.R.   Harold's   book   "Java   I/O"  
      public   static   void   copy(InputStream   in,   OutputStream   out)    
        throws   IOException   {  
   
          //   do   not   allow   other   threads   to   read   from   the  
          //   input   or   write   to   the   output   while   copying   is  
          //   taking   place  
   
          synchronized   (in)   {  
              synchronized   (out)   {  
   
                  byte[]   buffer   =   new   byte[256];  
                  while   (true)   {  
                      int   bytesRead   =   in.read(buffer);  
                      if   (bytesRead   ==   -1)   break;  
                      out.write(buffer,   0,   bytesRead);  
                  }  
              }  
          }  
      }    
  }  
  问题点数:100、回复次数:2Top

1 楼ducadi83((大饼))回复于 2006-03-16 23:35:53 得分 0

顶Top

2 楼kevinocean(老婆饼)回复于 2006-03-16 23:37:00 得分 100

可以用msxml试试看Top

相关问题

  • AXIS调用web service出错~!提示目标服务为null~~~????帮帮忙!
  • [求助]部署在jboss3.2.5上的应用通过Axis调用Web Service总是报错
  • Web Form 怎么调用COM+
  • WEB SERVICE 的IAPPSERVER调用
  • 如何调用web services
  • WinForm如何调用Web Service?
  • winForm调用web service问题!
  • 调用Web Services出错
  • web services 调用的问题!!
  • Axis下WebService的调用问题,急!!!

关键词

  • soap
  • xml
  • httpconn
  • soapaction
  • inputline
  • bout
  • setrequestproperty
  • bytesread
  • fin
  • copy

得分解答快速导航

  • 帖主:ducadi83
  • kevinocean

相关链接

  • CSDN Java频道
  • Java类图书
  • Java类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo