Http POST与GET请求JAVA实现

实现HTTP POST与GET请求,最近使用该工具类实现了调用websrvice接口.

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
import java.util.Map.Entry;

/***
 * <p>
 * Title: Http 客服端工具类
 * </p>
 * <p>
 * Description:
 * </p> 
 * @author:落叶飞
 * @version 1.0
 * @history: Created by 落叶飞 2015-3-13
 * @webio:<a href="http://weibo.com/lyfxinsui">七里外风车磨坊</a>
 */
public class HttpClientUtil {
	/***
	 * <p>
	 * Description:发送HTTP POST请求
	 * <p>
	 * 
	 * @param serverUrl
	 *            :服务端地址
	 * @param sendParms
	 *            :发送post请求参数,map中key为参数名,value为参数值
	 * @return
	 * @author 陈波 2015-3-11
	 */
	public final static String sendPostRequest(String serverUrl, Map<String, String> sendParms) {
		return sendRequest(serverUrl, sendParms, "POST");
	}

	/***
	 * <p>
	 * Description:发送HTTP GET请求
	 * <p>
	 * 
	 * @param serverUrl
	 *            :服务端地地址
	 * @param sendParms
	 *            :发送get请求参数,map中key为参数名,value为参数值
	 * @return
	 * @author 陈波 2015-3-11
	 */
	public final static String sendGetRequest(String serverUrl, Map<String, String> sendParms) {
		return sendRequest(serverUrl, sendParms, "GET");
	}

	/***
	 * 
	 * <p>
	 * Description:发送HTTP请求 POST 或者GET
	 * <p>
	 * 
	 * @param serverUrl
	 *            :请求服务地址
	 * @param sendParms
	 *            :请求参数和值
	 * @param sendMethod
	 *            :发请求类型 POST或者GET
	 * @return
	 * @author 陈波 2015-3-13
	 */
	public static final String sendRequest(String serverUrl, Map<String, String> sendParms, String sendMethod) {

		if (null != sendMethod && !"".equals(sendMethod) && ("post".equalsIgnoreCase(sendMethod) || "get".equalsIgnoreCase(sendMethod))) {

			StringBuffer resultBuffer = new StringBuffer();// 请求结果
			StringBuffer sendParmsBuffer = new StringBuffer();
			URL url = null;
			HttpURLConnection httpURLConnection = null; // http 连接对象
			OutputStreamWriter outputStreamWriter = null; // post 发送数据
			BufferedReader bufferedReader = null; // 按照行结果获取结果数据

			if (null != sendParms && 0 < sendParms.size()) {
				// 构造HTTP 请求参数格式
				int index = sendParms.size() - 1;
				for (Entry<String, String> objParms : sendParms.entrySet()) {
					sendParmsBuffer.append(objParms.getKey() + "=" + objParms.getValue());
					if (index != 0) {
						sendParmsBuffer.append("&");
					}
					index--;
				}
				if (sendMethod.toUpperCase().equals("GET")) {
					serverUrl = serverUrl + "?";
					serverUrl += sendParmsBuffer.toString();
				}
			}
			try {
				url = new URL(serverUrl);
				httpURLConnection = (HttpURLConnection) url.openConnection();
				httpURLConnection.setDoOutput(true);
				httpURLConnection.setDoInput(true);
				httpURLConnection.setRequestMethod(sendMethod.toUpperCase());
				httpURLConnection.setUseCaches(false);
				httpURLConnection.setInstanceFollowRedirects(false);
				httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
				httpURLConnection.connect();
				if (0 < sendParmsBuffer.length() && sendMethod.toUpperCase().equals("POST")) {
					outputStreamWriter = new OutputStreamWriter(httpURLConnection.getOutputStream(), "UTF-8");
					outputStreamWriter.write(sendParmsBuffer.toString());
					outputStreamWriter.flush();
				}
				bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream(), "UTF-8"));
				String line;
				while (null != (line = bufferedReader.readLine())) {
					resultBuffer.append(line);
				}
			} catch (MalformedURLException e) {
				e.printStackTrace();
				throw new RuntimeException("请求失败:" + e.getMessage());
			} catch (IOException e) {
				e.printStackTrace();
				throw new RuntimeException("请求失败:" + e.getMessage());
			} finally {
				if (null != httpURLConnection) {
					httpURLConnection.disconnect();
				}
				if (null != outputStreamWriter) {
					try {
						outputStreamWriter.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				if (null != bufferedReader) {
					try {
						bufferedReader.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}

			}
			return resultBuffer.toString();

		}
		return null;

	}

}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值