HttpConnection通信工具类

 

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 项目名称:HC360SSOSecurity    
 * 类名称:HttpConnectionUtil    
 * 类描述:HttpConnection通信工具类
 * 创建人:juapk
 * 创建时间:May 24, 2012 2:38:06 PM    
 * 修改人: 
 * 修改时间:May 24, 2012 2:38:06 PM    
 * 修改备注:    
 * @version  V1.0
 */
public class HttpConnectionUtil {
	private static Log logger = LogFactory.getLog(HttpConnectionUtil.class.getName());
	
	/**
	 * 以URL方式发送数据
	 * 
	 * @param urlStr
	 *            发送地址 www.juapk.com
	 * @param contentStr
	 *            发送内容 id=666888
	 * @param charset
	 *            发送字符集 utf-8
	 * @param sResult
	 *            返回数据Buffer
	 * @param connectTimeout
	 *            连接主机超时(单位:毫秒) 30
	 * @param readTimeout
	 *            从主机读取数据超时(单位:毫秒) 30
	 * @return boolean 发送是否成功 (true||false)
	 */
	public static boolean sendStrOfPost(String urlStr, String contentStr, String charset, StringBuffer sResult,int connectTimeout,int readTimeout) {
		boolean bResult = false;
		String charsetName = charset;
		URL url = null;
		HttpURLConnection httpConnection = null;
		InputStream httpIS = null;
		java.io.BufferedReader http_reader = null;
		try {
			url = new URL(urlStr);
			httpConnection = (HttpURLConnection) url.openConnection();

			// 设置连接主机超时(单位:毫秒)
			httpConnection.setConnectTimeout(connectTimeout);
			// 设置从主机读取数据超时(单位:毫秒)
			httpConnection.setReadTimeout(readTimeout);

			httpConnection.setRequestMethod("POST"); // POST方式提交数据
			httpConnection.setDoOutput(true);
			httpConnection.setRequestProperty("Content-Length", String.valueOf(contentStr.getBytes().length));
			PrintWriter out = null;
			out = new PrintWriter(new OutputStreamWriter(httpConnection.getOutputStream(), charsetName));// 此处改动
			// 发送请求
			out.print(contentStr);
			out.flush();
			out.close();
			int responseCode = httpConnection.getResponseCode();
			if (responseCode == HttpURLConnection.HTTP_OK) {
				// 发送正常
				bResult = true;

				// 读取数据
				httpIS = httpConnection.getInputStream();
				http_reader = new java.io.BufferedReader(new java.io.InputStreamReader(httpIS, charsetName));
				String line = null;
				while ((line = http_reader.readLine()) != null) {
					if (sResult.length() > 0) {
						sResult.append("\n");
					}
					sResult.append(line);
				}
				logger.info("[URL][response][success]" + sResult);
			} else {
				logger.info("[URL][response][failure][code : " + responseCode + " ]");
			}
		} catch (IOException e) {
			logger.error("[HttpUtil]sendStrOfPost error", e);
		}
		finally {
			try {
				if (http_reader != null)
					http_reader.close();
				if (httpIS != null)
					httpIS.close();
				if (httpConnection != null)
					httpConnection.disconnect();
			} catch (IOException e) {
				logger.error("[HttpUtil]finally error", e);
			}
		}
		return bResult;
	}
}
 

 

你可能感兴趣的:(Connection)