本文介绍了来自一个HttpURLConnection的多个请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用Java在一个HttpURLConnection中执行几个请求?
How can I do several request in one HttpURLConnection with Java?
URL url = new URL("http://my.com");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
HttpURLConnection.setFollowRedirects( true );
connection.setDoOutput( true );
connection.setRequestMethod("GET");
PrintStream ps = new PrintStream( connection.getOutputStream() );
ps.print(params);
ps.close();
connection.connect();
//TODO: do next request with other url, but in same connection
谢谢.
推荐答案
来自Javadoc:
该对象显然不打算重复使用.
The object apparently isn't meant to be re-used.
除了一点内存混乱和效率低下外,为您要发出的每个请求打开一个HttpURLConnection并没有什么大问题.但是,如果您想要大规模高效的网络IO,最好使用专门的库,例如 Apache HttpClient .
Aside from a little memory thrashing and inefficiency, there's no big problem with opening one HttpURLConnection for every request you want to make. If you want efficient network IO on a larger scale, though, you're better off using a specialized library like Apache HttpClient.
这篇关于来自一个HttpURLConnection的多个请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!