Applet HttpUrlConnection 分块响应问题
我有一个带有 HttpUrlConnection 到 IIS 6.0 服务器的 Applet。 服务器响应分块数据,但有时(某些浏览器)我遇到问题。 服务器答案保存在某个缓冲区中,并在服务器超时关闭连接时到达我。
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
有什么想法吗?
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");
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();
}
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 readLine() 中,你似乎做了一些奇怪的事情。
当
read==-1
(这意味着流结束)时,您将等待 150 秒,然后再设置errorConnection=true
,这允许readLine
以及随后的readData 和 while (!errorConnection & !trunkStop) { readData(); } 停止。这 150 秒可能比 IIS 服务器的连接超时时间还要长。
顺便提一句。您正在使用按位与运算符 (
&
),而不是逻辑与运算符 (&&
) 您确定这就是您想要的吗?检查差异。& java 按位与运算符
&& java McCarthy 和运算符
In
readLine()
you seem to be doing something strange.When
read==-1
(which means end of stream) you are waiting for 150 secondes before settingerrorConnection=true
which allowsreadLine
and subsequentlyreadData
and thewhile (!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
问题解决了!
客户端使用代理。 HTTPS 使用帮助
problem is solved!
The client uses a proxy. HTTPS using helped