Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在单个连接中发送多个post请求的HttpUrlConnection_Java_Http Post_Persistence_Httpconnection - Fatal编程技术网

Java 在单个连接中发送多个post请求的HttpUrlConnection

Java 在单个连接中发送多个post请求的HttpUrlConnection,java,http-post,persistence,httpconnection,Java,Http Post,Persistence,Httpconnection,我想在一个HTTP连接中进行多个post调用 我将发送Arraylist和httpConnection对象作为输入参数。 迭代ArrayList并将请求写入服务器。 我最终得到以下错误: Cannot write output after reading input. at sun.net.www.protocol.http.HttpURLConnection.getOutputStream0(Unknown Source) at sun.net.www.protocol.http.HttpUR

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

我将发送Arraylist和httpConnection对象作为输入参数。 迭代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;
}

如何克服这种情况?

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


你可能不应该在这里断开连接;而是由调用方决定。

根据HttpURLConnection的javadoc:

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

有关更多信息,请参阅

问题是一旦完成wr.flush,就无法执行conn.getOutputStream

如果要发送另一个post请求,必须创建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实例。即使您使用了其中的多个,也可以使用相同的套接字和底层TCP。因此,不要担心HttpURLConnection的多个实例。

嘿,我需要能够使用单个Httpurlcollection进行多个调用。这是我要找的,这是你要的连接代码:public HttpURLConnection connString tempurl,String apiKey,String GUID抛出IOException{HttpURLConnection conn=null;URL URL=new URLtempurl.replaceguid,GUID.toString;conn=HttpURLConnection URL.openConnection;conn.setDoOutputtrue;conn.setDoInputtrue;conn.setRequestPropertyapi-key,apiKey;conn.setRequestMethodPOST;conn.setRequestPropertyContent-Type,application/json;return conn;}根据新信息更新了代码。这将消除您遇到的错误。如果您想使用HTTP持久连接,请遵循您提供的链接中的指导原则。但不需要使用相同的HttpURLConnection实例来实现持久连接。