欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java使用post方式发送JSON数据乱码问题

程序员文章站 2022-05-08 13:30:22
...
	public static void sendPostJson(String ADD_URL, JSONObject obj){
		try {
			URL url = new URL(ADD_URL);
	        HttpURLConnection http = (HttpURLConnection) url.openConnection();
	        http.setDoOutput(true);  
	        http.setDoInput(true);  
	        http.setRequestMethod("POST");  
	        http.connect();  
	        OutputStreamWriter out = new OutputStreamWriter(http.getOutputStream(), "UTF-8"); 
	        
	//        String input = "data=[{\"Nianji\":\"高中2015届\",\"Banji\":\"元培书院\",\"Xuehao\":\"1525998\",\"Xingming\":\"姓名\",\"JianhurenShouji\":\"11111111111\",\"QingjiaLeixing\":\"病假\",\"KaishiShijian\":\"2014-05-24T00:00:00\",\"KaishiJie\":\"第1节\",\"JieshuShijian\":\"2014-05-24T00:00:00\",\"JieshuJie\":\"第1节\",\"QingjiaYuanyin\":\"原因\",\"key\":\"\",\"pagesize\":5}]"; 
	         
	        out.append(obj.toString());  
	        out.flush();  
	        out.close();  
	        BufferedReader reader = new BufferedReader(new InputStreamReader(http.getInputStream()));
	        String line;
	        StringBuffer buffer = new StringBuffer();
	        while ((line = reader.readLine()) != null) {
	            buffer.append(line);
	        }
	        reader.close();
	        http.disconnect();
	        System.out.println(buffer.toString());
		} catch (Exception e) {
            System.out.println(e);
        }
	}

转载于:https://my.oschina.net/hehongbo/blog/839820