来自一个 HttpURLConnection 的多个请求 [英] several requests from one HttpURLConnection

查看:32
本文介绍了来自一个 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:

From the Javadoc:

每个 HttpURLConnection 实例用于发出单个请求.

Each HttpURLConnection instance is used to make a single request.

该对象显然不能重复使用.

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 的多个请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆