发送http post请求json数据到后台

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/m0_37680622/article/details/76547195

	public void doPost(String str, Map<String, Object> map) {
		try {
			URL url = new URL(str);

			HttpURLConnection connection = (HttpURLConnection) url.openConnection();

			connection.setRequestMethod("POST");
			connection.setDoInput(true);
			connection.setDoOutput(true);
			connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
			connection.setRequestProperty("Accept", "application/json");

			String postData = JSONObject.fromObject(map).toString();
			connection.connect();
			OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
			writer.write(postData);
			writer.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

展开阅读全文

没有更多推荐了,返回首页