gpt4 book ai didi

Java httpURLConnection 线程化

转载 作者:行者123 更新时间:2023-11-30 09:38:13 24 4
gpt4 key购买 nike

简单问题:

是否可以同时请求多个 httpURLConnection?我正在创建一个工具来检查某个服务器上是否存在页面,目前,Java 似乎要等到每个 httpURLConnection 完成才能启动一个新页面。这是我的代码:

public static String GetSource(String url){
String results = "";
try{
URL SourceCode = new URL(url);
URLConnection connect = SourceCode.openConnection();
connect.setRequestProperty("Host", "www.someserver.com");
connect.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 5.1; rv:11.0) Gecko/20100101 Firefox/11.0");
connect.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
connect.setRequestProperty("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
connect.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
connect.setRequestProperty("Keep-Alive", "115");
connect.setRequestProperty("Connection", "keep-alive");
BufferedReader in = new BufferedReader(new InputStreamReader(connect.getInputStream(), "UTF-8"));
String inputLine;
while ((inputLine = in.readLine()) != null){
results += inputLine;
}
return results;
}catch(Exception e){
// Something's wrong
}
return results;
}

非常感谢!

最佳答案

是的,有可能,您发布的代码可以同时从多个线程调用。

关于Java httpURLConnection 线程化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10214636/

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