HttpURLConnection Cannot write output after reading input.

Because, after I used the int pipeline, I used the out pipeline again, so I reported an error and didn’t test… Again

public static String sendPostRequest(String path,String param) throws IOException {
		String sendUrlString=path+param;
		URL url = new URL(sendUrlString);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		//Set the post method request
		// Keep the timeout within 8S, since multi-threaded sending is not introduced. To prevent problems with RTX, the delay is too long, which is not good for user experience.
		// Generally, the response is very fast, so we don't consider using thread pools. There is no need to make it complicated.
		conn.setReadTimeout(3000); // Read timeout is 3 seconds.
		conn.setConnectTimeout(5000); // Connection timeout is 5 seconds.
		//conn.setRequestProperty("Accept-Charset", "utf-8");
		//conn.setRequestProperty("contentType", "utf-8");
		conn.setRequestProperty("Content-type", "application/x-java-serialized-object");
		//Set incoming parameters,post 
		conn.connect(); // actively establish indirect . , which is also established by default.
		// Pass in parameters
		// conn.getOutputStream().write(param.toString().getBytes());
		// Solve the Chinese garbage, there is no garbage under Linux, there is locally, I guess Linux has already transcoded it once for us.
		// And the default encoding under Linux is not the same as the local one, such as tomcat's, system's, not the default Chinese one, etc.
		// Since there is no garbled code under Linux, we can ignore it and modify the server's encoding if we want to later.
		// BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
		// Solve Chinese garbled code, there is garbled code under Linux, not locally
		// BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "GBK"));
		// The out pipe must be used before the int pipe. This is specified by HRRt
		// can be closed without initiative, it will be closed automatically by default. In particular, it is closed at the same time as the int pipe is closed.
		conn.getOutputStream().flush(); // This is a good idea to have
		 conn.getOutputStream().close(); // The close code can be left out.
		InputStreamReader isreader = new InputStreamReader(conn.getInputStream());
		BufferedReader reader = 
				new BufferedReader(isreader);
		String readingString= "";
		// After reading all the contents
		String readl=null;
		while (( readl=reader.readLine() )!=null) {
		// Must be used before the int pipe. , do not put it here, it will report an error: Cannot write output after reading input.
		// conn.getOutputStream().close(); 
		// Make it a habit to close the pipe.
		return readingString;


Similar Posts: