Interface docking problem

Find the information

Ruoyi's java sends a POST request to the post simulation request and
an exception occurs!

Problems reading the .properties file

Java simulates a front-end post request to obtain data, and an error is reported at the beginning. The link param contains -, which is? in the output. But writing the link directly in Java does not have this problem, that is, reading the .properties file PropertiesUtil, changing the byte stream to the character stream, and getting the link through the PropertiesUtil method is normal.

public class ProPertiesUtil {
    
    
	private static ProPertiesUtil configManager;
	private static Properties properties;
	//通过私有构造方法读取配置文件里的信息
	private ProPertiesUtil(String path){
    
    
		//String path="test.properties";
		//读取数据
		InputStream is= ProPertiesUtil.class.getClassLoader().getResourceAsStream(path);
		//通过Properties获取
		properties=new Properties();
		try {
    
    
	// 原来的字节流
	//			properties.load(is);
	// 现在的字符流
   //这样输入的时候就可以不是一个一个字节读,而是一个一个字符读,再加上是个Buffer,效率会高很多。
			BufferedReader bf = new BufferedReader(new InputStreamReader(is));
			properties.load(bf);
			is.close();
		} catch (IOException e) {
    
    
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

sendPost file problem

But I still reported an error. My colleague asked me to use Noy's Java to simulate the post request
. After using it, the ioc reported an error 400.
Inquired again that an exception occurred when sending a POST request!
The second Chinese parameter is garbled, it can be used after a trial, but it returns garbled. So I carefully compared the two files. Both the request and the recovery are in Chinese, so both have to be transcoded to UTF-8.
If you encounter a problem, calm down and watch it slowly. The foundation is very important, otherwise a transcoding problem will get you stuck.

    public static String sendPost3(String url, String param)
    {
    
    
//        PrintWriter out = null;
        OutputStreamWriter out = null;
//        因为字节-导致链接有错误,所以我改了编码为字符,现在再改回字节
        BufferedReader in = null;
        StringBuilder result = new StringBuilder();
        try
        {
    
    
            String urlNameString = url;
            log.info("sendPost - {}", urlNameString);
            URL realUrl = new URL(urlNameString);
//            URLConnection conn = realUrl.openConnection();
            HttpURLConnection conn =  (HttpURLConnection)realUrl.openConnection();

            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            conn.setRequestProperty("Accept-Charset", "utf-8");
            conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
//            conn.setRequestProperty("contentType", "utf-8");
            conn.setDoOutput(true);
            conn.setDoInput(true);
//            out = new PrintWriter(conn.getOutputStream());
//            out.print(param);
            // out = new OutputStreamWriter(conn.getOutputStream());
            out = new OutputStreamWriter(conn.getOutputStream(),"utf-8");
//            问题是因为HttpURLConnection接收数据的时候 字符集默认的是GBK 要转码UTF-8
            out.write(param);
            System.out.println("param------------------------"+param);
            out.flush();
            in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
     //    接受有中文还是需要字节码的UTF-8//         in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = in.readLine()) != null)
            {
    
    
                result.append(line);
            }
            log.info("recv - {}", result);
        }
        catch (ConnectException e)
        {
    
    
            log.error("调用HttpUtils.sendPost ConnectException, url=" + url + ",param=" + param, e);
        }
        catch (SocketTimeoutException e)
        {
    
    
            log.error("调用HttpUtils.sendPost SocketTimeoutException, url=" + url + ",param=" + param, e);
        }
        catch (IOException e)
        {
    
    
            log.error("调用HttpUtils.sendPost IOException, url=" + url + ",param=" + param, e);
        }
        catch (Exception e)
        {
    
    
            log.error("调用HttpsUtil.sendPost Exception, url=" + url + ",param=" + param, e);
        }
        finally
        {
    
    
            try
            {
    
    
                if (out != null)
                {
    
    
                    out.close();
                }
                if (in != null)
                {
    
    
                    in.close();
                }
            }
            catch (IOException ex)
            {
    
    
                log.error("调用in.close Exception, url=" + url + ",param=" + param, ex);
            }
        }
        return result.toString();
    }


Come on and hit the workers!

Guess you like

Origin blog.csdn.net/qq_39088110/article/details/115347253