栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java http请求工具类

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java http请求工具类

import java.io.BufferedReader;
import java.io.Closeable;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
import java.util.stream.Collectors;

public class NetworkUtil {
	public static String sendPost(String pathUrl, Map param) {
		StringBuffer result = new StringBuffer();
		HttpURLConnection httpConn = null;
		DataOutputStream dos = null;
		BufferedReader responseReader = null;
		try {
			URL url = new URL(pathUrl);
			httpConn = (HttpURLConnection) url.openConnection();

			httpConn.setDoOutput(true);
			httpConn.setDoInput(true);
			httpConn.setUseCaches(false);
			httpConn.setRequestMethod("POST");
			StringBuffer requestString = new StringBuffer("");
			if (param != null && !param.isEmpty()) {
				for (String key : param.keySet()) {
					requestString.append("&").append(key).append("=").append(param.get(key));
				}
			}

			httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
			httpConn.setRequestProperty("Connection", "Keep-Alive");
			httpConn.setRequestProperty("Charset", "UTF-8");

			dos = new DataOutputStream(httpConn.getOutputStream());
			dos.writeBytes(requestString.toString());
			dos.flush();

			int responseCode = httpConn.getResponseCode();

			if (HttpURLConnection.HTTP_OK == responseCode) {
				String readLine;
				responseReader = new BufferedReader(new InputStreamReader(httpConn.getInputStream(), "UTF-8"));
				while ((readLine = responseReader.readLine()) != null) {
					result.append(readLine);
				}
			}
		} catch (Exception e) {

		} finally {
			if (httpConn != null)
				httpConn.disconnect();
			close(dos);
			close(responseReader);
		}
		return result.toString();
	}

	public static String sendGet(String url, Map param) {
		String result = "";
		BufferedReader in = null;
		try {
			StringBuffer requestString = new StringBuffer("");
			if (param != null && !param.isEmpty()) {
				for (String key : param.keySet()) {
					requestString.append("&").append(key).append("=").append(param.get(key));
				}
			}
			String urlNameString = url
					+ (requestString.length() > 0 ? "?" + requestString.substring(1).toString() : "");

			URL realUrl = new URL(urlNameString);

			HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();
	
			connection.setRequestProperty("accept", "*/*");
			connection.setRequestProperty("connection", "Keep-Alive");
			connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
	
			connection.connect();
		
			in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
			String line;
			while ((line = in.readLine()) != null) {
				result += line;
			}
		} catch (Exception e) {

		} finally {
			close(in);
		}
		return result;
	}


	public static String sendJsonForWeixin(String pathUrl, String jsonStr) {
		StringBuffer result = new StringBuffer();
		HttpURLConnection httpConn = null;
		BufferedReader responseReader = null;
		PrintWriter out = null;
		try {
			URL url = new URL(pathUrl);
			httpConn = (HttpURLConnection) url.openConnection();

			httpConn.setDoOutput(true);
			httpConn.setDoInput(true);
			httpConn.setUseCaches(false);
			httpConn.setRequestMethod("POST");
			httpConn.setRequestProperty("Content-Type", "application/json");
			httpConn.setRequestProperty("Connection", "Keep-Alive");
			httpConn.setRequestProperty("Charset", "UTF-8");

			
			OutputStreamWriter outWriter = new OutputStreamWriter(httpConn.getOutputStream(), "utf-8");
			out = new PrintWriter(outWriter);
		
			out.print(jsonStr);
	
			out.flush();

			int responseCode = httpConn.getResponseCode();

			if (HttpURLConnection.HTTP_OK == responseCode) {
				String readLine;
				responseReader = new BufferedReader(new InputStreamReader(httpConn.getInputStream(), "UTF-8"));
				while ((readLine = responseReader.readLine()) != null) {
					result.append(readLine);
				}
			}
		} catch (Exception e) {

		} finally {
			if (httpConn != null)
				httpConn.disconnect();
			close(responseReader);
			close(out);
		}
		return result.toString();
	}


	public static String sendJsonByWriter(String pathUrl, String jsonStr) {
		StringBuffer result = new StringBuffer();
		HttpURLConnection httpConn = null;
		BufferedReader responseReader = null;
		PrintWriter out = null;
		try {
			URL url = new URL(pathUrl);
			httpConn = (HttpURLConnection) url.openConnection();

			httpConn.setDoOutput(true);
			httpConn.setDoInput(true);
			httpConn.setUseCaches(false);
			httpConn.setRequestMethod("POST");
			httpConn.setRequestProperty("Content-Type", "application/json");
			httpConn.setRequestProperty("Connection", "Keep-Alive");
			httpConn.setRequestProperty("Charset", "UTF-8");

			
			OutputStreamWriter outWriter = new OutputStreamWriter(httpConn.getOutputStream(), "utf-8");
			out = new PrintWriter(outWriter);
	
			out.print(jsonStr);
			
			out.flush();

			int responseCode = httpConn.getResponseCode();

			if (HttpURLConnection.HTTP_OK == responseCode) {
				String readLine;
				responseReader = new BufferedReader(new InputStreamReader(httpConn.getInputStream(), "UTF-8"));
				while ((readLine = responseReader.readLine()) != null) {
					result.append(readLine);
				}
			}
		} catch (Exception e) {

		} finally {
			if (httpConn != null)
				httpConn.disconnect();
			close(responseReader);
			close(out);
		}
		return result.toString();
	}

	public static String sendJson(String pathUrl, String jsonStr) {
		HttpURLConnection httpConn = null;
		try {
			URL url = new URL(pathUrl);
			httpConn = (HttpURLConnection) url.openConnection();

			httpConn.setDoOutput(true);
			httpConn.setDoInput(true);
			httpConn.setUseCaches(false);
			httpConn.setRequestMethod("POST");

			httpConn.setRequestProperty("Content-Type", "application/json");
			httpConn.setRequestProperty("Connection", "Keep-Alive");
			httpConn.setRequestProperty("Charset", "UTF-8");

			httpConn.getOutputStream().write(jsonStr.getBytes("utf-8"));

			int responseCode = httpConn.getResponseCode();

			String response = new BufferedReader(new InputStreamReader(httpConn.getInputStream(), "UTF-8")).lines()
					.collect(Collectors.joining());

			if (HttpURLConnection.HTTP_OK != responseCode) {
				throw new RuntimeException("请求返回状态码为:" + responseCode + ",响应内容:" + response + ",没有处理流程");
			}

			return response;
		} catch (Exception e) {
			throw new RuntimeException("发送Json请求错误", e);
		} finally {
			if (httpConn != null)
				httpConn.disconnect();
		}
	}

	public static void close(Closeable obj) {
		try {
			obj.close();
		} catch (Exception e) {
		}
	}
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/733244.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号