Http requests a long wait for no result to return a solution

  • 2021-12-21 04:39:35
  • OfStack

Http requests a long wait for no result to return a solution

Today, I encountered a wonderful problem. This program is mainly used to call the interface to collect data. However, something bad happened is that there are many processes started in the server, and 1 can't be finished. I can't use kill to kill all this part of the process.

After analyzing the next program, I wrote a test. The local run still waits for a long time, can't run, and doesn't throw an exception. Finally, I found that the problem lies in sending the request. 1 is waiting for the response of the other server. Because the connection is keep-alive, there is no timeout here, so that the program will wait for a long time.

After testing, it is found that there are too many open processes because there is no timeout. I hope readers will pay attention to this part, otherwise, it is easy for the author to encounter this kind of problem. The final problem should be caused by an exception from the interface provider.

public static String sendPost(String url, String param) {
      PrintWriter out = null;
      BufferedReader in = null;
      String result = "";
      try {
        URL realUrl = new URL(url);
        //  Open and URL Connection between 
        URLConnection conn = realUrl.openConnection();
        //  Setting Common Request Properties 
        conn.setRequestProperty("accept", "*/*");
        conn.setRequestProperty("connection", "Keep-Alive");
            "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
        //  Send POST The request must be set with the following two lines 
        //  Get URLConnection The output stream corresponding to the object 
        out = new PrintWriter(conn.getOutputStream());
        //  Send request parameters 
        // flush Buffering of output stream 
        //  Definition BufferedReader Input stream to read URL Response of 
        in = new BufferedReader(
            new InputStreamReader(conn.getInputStream()));
        String line;
        while ((line = in.readLine()) != null) {
          result += line;
      } catch (Exception e) {
        System.out.println(" Send  POST  Exception in request! ");
      // Use finally Block to close the output stream, the input stream 
        catch(IOException ex){
      return result;

Thank you for reading, hope to help everyone, thank you for your support to this site!

Related articles: