gpt4 book ai didi

java - 代理人。思考过程。我怎样才能杀死他们?

转载 作者:行者123 更新时间:2023-12-01 15:12:27 24 4
gpt4 key购买 nike

有时,我的程序会通过代理服务器并读取舞会缓冲区内容来思考更多时间......直到我关闭它们。如何设置程序代码,在几秒钟内如果没有来自服务器的任何答复,则采取另一台服务器?

URL url = new URL(linkCar);
String your_proxy_host = new String(proxys.getValueAt(xProxy, 1).toString());
int your_proxy_port = Integer.parseInt(proxys.getValueAt(xProxy, 2).toString());
Proxy proxy = null;
// System.out.println(proxys.getValueAt(xProxy, 3).toString());
// if (proxys.getValueAt(xProxy, 3).toString().indexOf("HTTP") > 0)
// {
proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(your_proxy_host, your_proxy_port));
// } else {
// proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(your_proxy_host, your_proxy_port));
// }

HttpURLConnection connection = (HttpURLConnection)url.openConnection(proxy);
connection.setConnectTimeout(1000);
connection.connect();

String line = null;
StringBuffer buffer_page = new StringBuffer();
BufferedReader buffer_input = new BufferedReader(new InputStreamReader(connection.getInputStream(),"cp1251"));
int cc = 0;

//this is thinking place!!!
while ((line = buffer_input.readLine()) != null && cc < 7000) {
buffer_page.append(line);
cc++;
}

doc = Jsoup.parse(String.valueOf(buffer_page));
connection.disconnect();

我尝试使用计数器,但它不起作用...我可以使用什么异常来通过我的控制捕获这种情况?

最佳答案

您需要使用URLConnection.setReadTimeout 。从规范来看,

Sets the read timeout to a specified timeout, in milliseconds. A non-zero value specifies the timeout when reading from Input stream when a connection is established to a resource. If the timeout expires before there is data available for read, a java.net.SocketTimeoutException is raised. A timeout of zero is interpreted as an infinite timeout.

如您所见,读取超时将抛出 SocketTimeoutException ,您可以适本地捕获,例如

try (BufferedReader buffer_input = new BufferedReader(
new InputStreamReader(connection.getInputStream(), "cp1251"))) {
String line;
while ((line = buffer_input.readLine()) != null) {
buffer_page.append(line);
}
} catch (SocketTimeoutException ex) {
/* handle time-out */
}

请注意,如上所述使用 readLine 时需要小心 - 这将从输入中删除所有 \r\n .

关于java - 代理人。思考过程。我怎样才能杀死他们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12151370/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com