2016-05-23 138 views
1

I`ve打開HttpURLConnection的連接,打開它,並通過它寫道:我怎樣才能重新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(); 

一段時間之後,認證令牌已經過期,輸出流已被關閉。因此,在試圖寫I`ve遇到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);

回答

0

你不能。您必須爲相同的URL創建一個新的。

但是在幕後有TCP連接池。