【问题标题】:HttpUrlConnection that sends multiple post request in single connection在单个连接中发送多个发布请求的 HttpUrlConnection
【发布时间】:2017-01-25 04:24:40
【问题描述】:

我想在单个 HTTP 连接中进行多个 post 调用,

  1. 我将发送一个Arraylist<String>httpConnection 对象作为输入参数。
  2. 迭代ArrayList并将请求写入服务器。

我最终得到以下错误:

Cannot write output after reading input.
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream0(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(Unknown Source)

这是我的代码。我正在使用来完成上述任务。

public boolean sendToLogsAPI(ArrayList<String> logList, HttpURLConnection conn) throws IOException
{
    try 
    {
         DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
         for(int i=0; i<logList.size(); i++)
         {
             wr = new DataOutputStream(conn.getOutputStream());
             wr.writeBytes(logList.get(i));
             wr.flush();
             int nothing = conn.getResponseCode();
             String morenothing = conn.getResponseMessage();
         }   
         wr.flush();
         wr.close();
    }
    catch (Exception e) 
    {
      e.printStackTrace();
    } 
    finally 
    {
      if(conn != null) 
      {
        conn.disconnect(); 
      }
    }

    return false;
}

我该如何克服这种情况?

【问题讨论】:

    标签: java http-post persistence httpconnection


    【解决方案1】:

    您正在使用此方法关闭连接,因此您不能使用相同的 HttpURLConnection conn 调用它两次

    您可能不应该在conn.disconnect(); 此处断开连接,而是将其留给呼叫者。

    【讨论】:

      【解决方案2】:

      根据HttpURLConnection的javadoc:

      每个 HttpURLConnection 实例用于发出单个请求

      请参阅this 了解更多信息。

      问题是,一旦你完成了wr.flush(),你就不能再做conn.getOutputStream()

      如果你想发送另一个帖子请求,你必须创建一个HttpURLConnection 的新实例。你可以通过多种方式做到这一点。一种方法是创建一个方法getHttpURLConnection(),它每次都会提供新的连接[如果您展示如何创建HttpURLConnection 的实例并传递给方法sendToLogsAPI(),那么我可以向您展示getHttpURLConnection() 的实现以及。]并修改现有代码如下:

      public boolean sendToLogsAPI(ArrayList<String> logList) throws IOException
      {
          DataOutputStream wr = null;
          HttpURLConnection conn = null;
      
          try 
          {
               for(int i=0; i<logList.size(); i++)
               {
                   conn = conn("<Some-URL>","<API-Key>","<GUID>");
                   wr = new DataOutputStream(conn.getOutputStream());
                   wr.writeBytes(logList.get(i));
                   wr.flush();
                   int nothing = conn.getResponseCode();
                   String morenothing = conn.getResponseMessage();
               }   
               if(wr != null) {
                   wr.close();
               }
          }
          catch (Exception e) 
          {
            e.printStackTrace();
          } 
          finally 
          {
            if(conn != null) 
            {
              conn.disconnect(); 
            }
          }
      
          return false;
      }
      

      我想问你的另一个问题是你为什么要使用相同的HttpURLConnection 实例。即使您使用其中的多个,也可以使用相同的Socket(和底层 TCP)。所以不用担心HttpURLConnection 的多个实例。

      【讨论】:

      • 嘿,我需要能够使用单个 Httpurlcollection 进行多次调用。这就是我要找的docs.oracle.com/javase/8/docs/technotes/guides/net/…
      • 这是您要求的连接代码: public HttpURLConnection conn(String tempurl, String apiKey, String GUID) throws IOException { HttpURLConnection conn = null; URL url = new URL(tempurl.replace("guid", GUID).toString()); conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setDoInput(true); conn.setRequestProperty("api-key", apiKey); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/json");返回连接; }
      • 根据新信息更新了代码。这将消除您遇到的错误。如果您想使用 HTTP 持久连接,请遵循您提供的链接中的指南。但是你不需要有相同的 HttpURLConnection 实例来拥有持久连接。
      猜你喜欢
      • 2015-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多