用于多个帖子请求的java http Url连接

时间:2016-09-19 18:39:09

标签: java post httpurlconnection

我需要有关持久性httpurlconnection

的帮助

我的方法标题看起来像

public boolean sendToAPI(ArrayList logs) {

}

我需要能够通过迭代日志列表打开HttpUrlConnection并调用多个post请求,然后关闭HttpurlConnection。

我总是得到以下错误:

读取输入后无法写入输出。 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 void sendEvents(ArrayList logList,HttpURLConnection conn)抛出IOException     {

    DataOutputStream wr = null;


    try 
    {
         for(int i=0; i<logList.size(); i++)
         {
          conn.connect();   
             wr = new DataOutputStream(conn.getOutputStream());
             wr.writeBytes(logList.get(i));
             wr.flush();
             int nothing = conn.getResponseCode();
             String morenothing = conn.getResponseMessage();
             wr.close();
             conn.disconnect();
         }   
         if(wr != null) {
             wr.close();
         }
    }
    catch (Exception e) 
    {
      e.printStackTrace();
    } 
    finally 
    {
      if(conn != null) 
      {
        conn.disconnect(); 
      }
    }

0 个答案:

没有答案