免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1063 | 回复: 0
打印 上一主题 下一主题

SOAPClient4XG [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-12-11 15:01 |只看该作者 |倒序浏览
/**
* 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  
        String SOAPUrl      = "
http://www.mobi-k.com:8088/mbk/services/Calc
";
        String xmlFile2Send = "add.xml";
    String SOAPAction = "add";
   
        // 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);
        }
      }
    }
  }
}


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/4864/showart_440227.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP