2009-10-19 41 views
0

我有一個Applet與HttpUrlConnection到IIS 6.0服務器。 服務器響應塊數據,但有時(一些瀏覽器),我有一個問題。 服務器答案粘在某個緩衝區中,並在服務器通過超時關閉連接時到達我。小程序HttpUrlConnection分塊響應問題

url = new URL(urlStr); 
huc = (HttpURLConnection) url.openConnection(); 
huc.setDefaultUseCaches(false); 
huc.setAllowUserInteraction(true); 
huc.setDoInput(true); 
huc.setUseCaches(false); 
huc.setRequestProperty("Pragma", "no-cache"); 
huc.setRequestProperty("Cache-Control", "no-cache"); 
huc.setRequestProperty("Expires", "-1"); 
huc.setRequestProperty("Content-type", "text/html"); 
InputStream is = huc.getInputStream(); 
... 
while (!trunkStop) { 
    while (errorConnection && !trunkStop) { 
    connectToServer(); 
    if (errorConnection) { 
     sleepThread(); 
    } 
    } 

    while (!errorConnection && !trunkStop) { 
      readData(); 
    } 
... 
} 
... 
void readData() { 
    if (trunkStop) { 
     return; 
    } 
    try { 
     readLine(); 
     addTask();//put to task queue 
    } catch (Exception e) { 
     getLogger().log(Level.WARNING, "Error connection..."); 
    } 
} 
... 
String readLine() throws IOException, InterruptedException { 
    sb = new StringBuilder(); 
    int prev = -1; 
    while (!errorConnection && !trunkStop) { 
     read = is.read(); 
     if (read == -1) { 
      if (System.currentTimeMillis() - timer > 150000) { 
       getLogger().log(Level.SEVERE, "RECONNECT BY TIMEOUT"); 
       errorConnection = true; 
      } 
      Thread.sleep(10); 
      continue; 
     } else if (read == 13) { 

     } else if (read == 10 && prev == 13) { 
      break; 
     } else { 
      sb.append((char) read); 
      System.out.print((char) read); 
     } 
     prev = read; 
     timer = System.currentTimeMillis(); 
    } 
    return sb.toString(); 
} 

某些瀏覽器緩衝區,我不知道。 此時IIS已經發送答案,這是客戶端問題, IE7,java 1.6.0_16

任何想法?

+2

任何代碼向我們展示? 你是什麼意思與某些緩衝區(哪裏是緩衝區IIS或小應用程序)? 哪些版本的IIS,瀏覽器,Java? – jitter 2009-10-19 14:40:24

回答

0

問題就解決了!

客戶端使用代理。使用HTTPS幫助

0

readLine()你似乎在做一些奇怪的事情。

read==-1時(這意味着流的結尾)在設置errorConnection=true其允許readLine並隨後readDatawhile (!errorConnection & !trunkStop) { readData(); }停止之前正在等待150個secondes。

這150次端可能比IIS服務器的連接超時長。


Btw。您正在使用按位運算符(&)而不是邏輯運算符(&&),您確定這就是您想要的嗎?檢查差異。

& java bit-wise and operator

&& java McCarthy and operator

+0

理論上來說,你是對的,但實際上當所有這些都是在瀏覽器中執行時,InputStream在流未關閉時可以返回-1。特別是,當我們使用https連接。程序運行時,此蒸汽必須一直存在。服務器不斷地將數據(如時間)放入流中。 至於&&,你是對的,謝謝。 – mboadas 2009-10-19 18:14:03

+0

Danke schon fur Ihre Antwort,ich freue mich!維也納ist ein schone Stadt – mboadas 2009-10-19 18:16:14