package com.winsun.utils;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;


public class HttpService {
	
	public static String sendPost(String url, String param) throws ParseException, IOException{
		String	responseString = "";
		
		HttpPost httpPost = new HttpPost(url);
		httpPost.setEntity(new StringEntity(param, HTTP.UTF_8));
		HttpResponse httpResponse = new DefaultHttpClient().execute(httpPost);
		try{
			int statusCode = httpResponse.getStatusLine().getStatusCode();
			if (statusCode == 200) {
				responseString = EntityUtils.toString(httpResponse.getEntity());
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return responseString;
	}
	
	public InputStream post1(String param, String urlStr) {  
  
        InputStream is = null;  
        try {  
            byte[] xmlData = param.getBytes("UTF-8");  
            URL url = new URL(urlStr);  
            URLConnection urlCon = url.openConnection();  
            urlCon.setDoOutput(false);  
            urlCon.setDoInput(true);  
            urlCon.setConnectTimeout(40000);  
            urlCon.setReadTimeout(40000);  
            urlCon.setUseCaches(false);  
//          urlCon.setRequestProperty("Content-Type", contentType);  
//          urlCon.setRequestProperty("Content-length",  
//                  String.valueOf(xmlData.length));  
//          // urlCon.setFixedLengthStreamingMode(xmlData.length);  
//          DataOutputStream printout = new DataOutputStream(  
//                  urlCon.getOutputStream());  
//          printout.write(xmlData);  
//          printout.flush();  
//          printout.close();  
            is = urlCon.getInputStream();  
        } catch (IOException e) {  
            e.printStackTrace();  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return is;  
    }  
}
