我已经打开HttpURLConnection连接,打开它并通过它编写:

HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setDoOutput(true);
httpCon.setRequestMethod("PUT");
httpCon.addRequestProperty("X-Auth-Token", getAuthToken());
httpCon.addRequestProperty("Content-Type", contentType);

httpCon.setDoInput(true);
httpCon.setRequestProperty("Connection", "close");
httpCon.setReadTimeout(READ_TIMEOUT);
httpCon.setRequestProperty("Transfer-Encoding","chunked");
httpCon.setDoOutput(true);
httpCon.setChunkedStreamingMode(STREAMING_CHUNK);
OutputStream out   = httpCon.getOutputStream();


一段时间后,身份验证令牌已过期,并且输出流已关闭。因此,在尝试写作时,我遇到了IOException
为了解决这个问题,我尝试重新验证并再次写:

private void ReAuthenticate()
{
   mHttpCon.addRequestProperty("X-Auth-Token", getNewToken());
   mOutputStream = mHttpCon.getOutputStream();
}
    public void write(byte[] b) throws IOException {
      try{
         mOutputStream.write(b);
      }
      catch (IOException ex)
      {
          ReAuthenticate();
      }
  }


但是什么都没写?
有什么事吗是否保存了httCon的所有属性?例如httpCon.setDoOutput(true);吗?

最佳答案

你不能您必须为相同的URL创建一个新的。

但是,在后台有TCP连接池。

10-06 11:01