Java发送GET请求和POST请求小工具

Java发送GET请求和POST请求小工具

写了一个比较通用的请求工具,请求参数和请求头也容易添加并发送请求,也可以不添加请求参数。我也对请求到的类型进行了分类:text,json,image,如有特殊情况可以自己添加。
目前仅支持GET、POST请求,如有特殊情况可以自己添加请求方式。感觉不错就点个赞呗!

小工具源码如下

/**
	 * 
	 * @param url				请求地址
	 * @param method			请求方法
	 * @param paramsMap			请求参数
	 * @param headerMap			请求头
	 * @return
	 */
public static Map<String, Object> sendHttpRequest(String urlStr, String method,
		Map<String, String> paramsMap, Map<String, String> headerMap) {
	URL url = null;
	URLConnection connection = null;

	BufferedReader in = null;
	PrintWriter out = null;
	String param = "";

	//准备接收的map
	Map<String, Object> map = new LinkedHashMap<String, Object>();

	//用来接收主要回复内容的容器
	List<String> contentTypes = null;
	List<String> cookie = null;
	//得到的结果
	String result = "";

	//判断是否有参数
	if (paramsMap != null) {
		//将参数转换为字符串
		for (Map.Entry<String, String> entry : paramsMap.entrySet()) {
			String key = entry.getKey();
			String value = entry.getValue();
			param += key + "=" + value + "&";
		}
		param = param.substring(0, param.length() - 1);
	}

	try {

		//判断是什么请求
		if ("GET".equalsIgnoreCase(method)) {
			//GET请求
			param = "?" + param;
			url = new URL(urlStr + "?" + param);
			connection = url.openConnection();

			//设置超时时间
			connection.setConnectTimeout(5000);
			connection.setReadTimeout(5000);

			//判断headersList是否为空,设置请求头
			if (headerMap != null) {
				for (Map.Entry<String, String> entry : headerMap.entrySet()) {
					connection.setRequestProperty(entry.getKey(), entry.getValue());
				}
			}
			connection.setRequestProperty("accept", "*/*");
			connection.setRequestProperty("connection", "Keep-Alive");
			connection.setRequestProperty("user-agent", USER_AGENT);

			// 建立连接
			connection.connect();

		} else if ("POST".equalsIgnoreCase(method)) {
			//POST请求
			url = new URL(urlStr);

			//判断headersList是否为空,设置请求头
			if (headerMap != null) {
				for (Map.Entry<String, String> entry : headerMap.entrySet()) {
					connection.setRequestProperty(entry.getKey(), entry.getValue());
				}
			}

			connection.setRequestProperty("accept", "*/*");
			connection.setRequestProperty("connection", "Keep-Alive");
			connection.setRequestProperty("user-agent", USER_AGENT);
			connection.setDoOutput(true);
			connection.setDoInput(true);

			// 获取URLConnection对象对应的输出流
			out = new PrintWriter(connection.getOutputStream());

			// 发送请求参数
			out.print(param);
			out.flush();
		}

		// 获取所有响应头字段
		Map<String, List<String>> headerFields = connection.getHeaderFields();
		for (Map.Entry<String, List<String>> entry : headerFields.entrySet()) {
			String key = entry.getKey();
			List<String> value = entry.getValue();
			System.out.println(key + "---------->" + value);
			if ("Content-Type".equals(key)) {
				contentTypes = value;
			} else if ("Set-Cookie".equals(key)) {
				cookie = value;
			}
		}

		//读取URL的响应
		in = new BufferedReader(
				new InputStreamReader(connection.getInputStream(), "utf-8"));
		String line;
		while ((line = in.readLine()) != null) {
			result += line;
		}
		if (in != null) {
			in.close();
		}

		//4.处理结果
		for (String contentType : contentTypes) {
			if (contentType.contains("text")) {

				System.out.println("是文本类型");

				map.put("text", result);

			} else if (contentType.contains("json")) {

				System.out.println("是JSON类型");

				map.put("json", result);

			} else if (contentType.contains("image")) {

				System.out.println("是图片类型");

			}
		}
		map.put("contentTypes", contentTypes);
		map.put("cookie", cookie);
	} catch (IOException e) {
		e.printStackTrace();
	} finally {
		if (in != null) {
			try {
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if (out != null) {
			out.close();
		}
	}
	return map;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值