SoapClient.java 6.5 KB
package com.sitech.ismp.coll.tivoli.db2;


import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

/**
 * 该类为一个工具类
 * 负责将soap内容发给soap server
 * 得到返回结果
 * @author winnerbao
 *
 */

public class SoapClient {


	private String SOAPUrl;

	public SoapClient(String SOAPUrl){
		this.SOAPUrl=SOAPUrl;

	}
	
	/**
	 * 功能:按指定编码格式返回字符串
	 * SoapReq is soap request
	 * strCharSet 是编码格式 例如 GBK、GB2312、UTF-8等.
	 * @param strSoapReq
	 * @param strCharSet
	 * @return
	 * @throws java.io.IOException
	 */
	public String getSoapStringResponse(String strSoapReq,String strCharSet)throws IOException{
		byte[] b = strSoapReq.getBytes();
		InputStream ins = this.getSoapResponse(b);
		
		String result = this.stream2String(ins,strCharSet);
        ins.close();
        return result;
	}//:~ getSoapStringResponse
	
	public String getSoapStringResponse(String strSoapReq)throws IOException{
		byte[] b = strSoapReq.getBytes();
		InputStream ins = this.getSoapResponse(b);
		
		String result = this.stream2String(ins);
        
        ins.close();
        
        return result;
	}//:~ getSoapStringResponse
	
	
	private InputStream getSoapSteamResponse(String strSoapReq)throws IOException	{
		byte[] b = strSoapReq.getBytes();
		return this.getSoapResponse(b);
	}//:~ end getSoapSteamResponse
		
	/**
	 *inStreamSoapReq
	 */
	private String getSoapStringResponse(InputStream inStreamSoapReq) throws IOException	{
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        // Copy the SOAP req stream to the open connection.
        copy(inStreamSoapReq,bout);
        byte[] b = bout.toByteArray();
        
		InputStream ins = this.getSoapResponse(b);
		
		String reult = this.stream2String(ins);
        
        ins.close();
        
        return reult;
	}//:~ end getSoapStringResponse
	
	/**
	 *inStreamSoapReq
	 */
	private InputStream getSoapStreamResponse(InputStream inStreamSoapReq) throws IOException	{
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        // Copy the SOAP req stream to the open connection.
        copy(inStreamSoapReq,bout);
        byte[] b = bout.toByteArray();
        
		return this.getSoapResponse(b);
	}//:~ 	getSoapStreamResponse
	
	/**
	 * 功能:按照指定的编码格式转换成字符串
	 * @param inStream
	 * @param strCharSet
	 * @return
	 * @throws java.io.IOException
	 */
	private String stream2String(InputStream inStream,String strCharSet)throws IOException{
        InputStreamReader isr =   new InputStreamReader(inStream,strCharSet);
        BufferedReader in = new BufferedReader(isr);

        String inputLine;

		StringBuffer buffer = new StringBuffer();
        while ((inputLine = in.readLine()) != null)
        {
            //System.out.println(inputLine);
            buffer.append(inputLine);
            buffer.append('\n');
		}
		
        return buffer.toString();	
	}//:~ end stream2String
	
	private String stream2String(InputStream inStream)throws IOException{
        InputStreamReader isr =   new InputStreamReader(inStream);
        BufferedReader in = new BufferedReader(isr);

        String inputLine;

		StringBuffer buffer = new StringBuffer();
        while ((inputLine = in.readLine()) != null)
        {
            //System.out.println(inputLine);
            buffer.append(inputLine);
            buffer.append('\n');
		}
		
        return buffer.toString();	
	}//:~ end stream2String

	private InputStream getSoapResponse(byte[] bytesSoapReq)throws IOException
	{
		String SOAPAction="";
        // Create the connection where we're going to send the file.
        URL url = new URL(SOAPUrl);
        URLConnection connection = url.openConnection();
        HttpURLConnection httpConn = (HttpURLConnection) connection;	

        // Set the appropriate HTTP parameters.
        httpConn.setRequestProperty( "Content-Length",
                                     String.valueOf( bytesSoapReq.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( bytesSoapReq );    
        out.close();

        // Read the response and write it to standard out.

        InputStream is = httpConn.getInputStream();
		
        //httpConn.disconnect();
        
		return is;
	}//:~ end getSoapResponse

  // copy method from From E.R. Harold's book "Java I/O"
  public 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);
        }
      }
    }
  }//:~ end copy
  
  public static void main(String[] args)
  {
  	/*String URL="http://172.16.9.98:1920///cms/soap";
  	String req="<CT_Get><userid>sysadmin</userid><password></password><object>ManagedSystem</object><target>ManagedSystemName</target></CT_Get>";
  	SoapClient client = new SoapClient(URL);
  	String out = client.getSoapStringResponse(req);
  	System.out.println(out);*/
	/*FileReader ufr = new FileReader("d:\\u.TXT");
	FileReader afr = new FileReader("d:\\a.TXT");
	BufferedReader ubr = new BufferedReader(ufr);
	BufferedReader abr = new BufferedReader(afr);
	String uvalue = "";
	String avalue = "";
	String uline = "";
	String aline = "";
	
	StringBuffer usb = new StringBuffer();
	StringBuffer asb = new StringBuffer();
	
	while((uline=ubr.readLine())!=null)
	{
		usb.append(uline);
	}
	uvalue = usb.toString();
	
	while((aline=abr.readLine())!=null)
	{
		asb.append(aline);
	}
	avalue = asb.toString();
	
	ubr.close();
	abr.close();
	ufr.close();
	afr.close();
	
	byte [] u = uvalue.getBytes();
	System.out.println("u byte line : " + u.length);
	
	byte [] a = avalue.getBytes();
	System.out.println("a byte line : " + a.length);*/
	String soapurl = "SOAP_URL=http://10.110.16.201:1920///cms/soap";
	SoapClient soapClient = new SoapClient(soapurl);
   } 	
}