package org.cs.util;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;

public class HttpPostXML {
	 public static String post(String url,String xmlString){  
	          
	        HttpClient client = new HttpClient();  
	        PostMethod myPost = new PostMethod(url);  
	        client.getParams().setSoTimeout(300*1000);  
	        String responseString = null;  
	        try{  
	            myPost.setRequestEntity(new StringRequestEntity(xmlString,"text/xml","utf-8"));  
	            int statusCode = client.executeMethod(myPost);  
	            if(statusCode == HttpStatus.SC_OK){  
	                BufferedInputStream bis = new BufferedInputStream(myPost.getResponseBodyAsStream());  
	                byte[] bytes = new byte[1024];  
	                ByteArrayOutputStream bos = new ByteArrayOutputStream();  
	                int count = 0;  
	                while((count = bis.read(bytes))!= -1){  
	                    bos.write(bytes, 0, count);  
	                }  
	                byte[] strByte = bos.toByteArray();  
	                responseString = new String(strByte,0,strByte.length,"utf-8");  
	                bos.close();  
	                bis.close();  
	            }  
	        }catch (Exception e) {  
	            e.printStackTrace();  
	        }  
	        myPost.releaseConnection();  
	        client.getHttpConnectionManager().closeIdleConnections(0);  
	        return responseString;  
	    } 
}
