Applet HttpUrlConnection 分块响应问题

发布于 2024-08-08 03:30:44 字数 1892 浏览 4 评论 0原文

我有一个带有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

御守 2024-08-15 03:30:44

在 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 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

↘人皮目录ツ 2024-08-15 03:30:44

问题解决了!

客户端使用代理。 HTTPS 使用帮助

problem is solved!

The client uses a proxy. HTTPS using helped

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文