Applet HttpUrlConnection Chunked Response problem
I have an Applet with HttpUrlConnection to IIS 6.0 Server. Server response chunked data, but sometimes (some browsers) i have a problem. Server answers stick in certain buffer, and arrive at me when the server close connection by timeout.
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");
InputS开发者_Go百科tream 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();
}
certain browser buffer, i don't know. At this moment IIS already send answers, this is client problem, IE7, java 1.6.0_16
Any ideas ?
In readLine()
you seem to be doing something strange.
When read==-1
(which means end of stream) you are waiting for 150 secondes before setting errorConnection=true
which allows readLine
and subsequently readData
and the while (!errorConnection & !trunkStop) { readData(); }
to stop.
These 150 secondes could be longer than the connection timeout of the IIS server.
Btw. you are using the bit-wise and operator (&
) instead of the logical and operator (&&
) are you sure that's what you want? Check the difference.
& java bit-wise and operator
&& java McCarthy and operator
problem is solved!
The client uses a proxy. HTTPS using helped
精彩评论