How to reset URLConnection in java?

时间秒杀一切 提交于 2019-12-08 01:32:36

问题


is there any way to reset the connection,so that i can read from starting of the document? It is a slow process that reconnecting to read from sarting,i want to read a file 20 times per second through http,is there any other way to do this?


回答1:


URLConnection is an easy but somewhat "dumb" class. I can suggest you to do it the Java way: instantiate a new URLConnection. ;) Java is not really a language that encorages reusage of objects.

You can try resetting the input stream, see the docs for more info, but I don't think it will do another GET. Who knows how the concrete implementation behind InputStream and OutputStream of URLConnections is done.

EDIT: I don't guarantee anything, but you could also try to disconnect() and then connect() again. Never tried it, see if it works.




回答2:


By default there is no way. If you really want this feature, you have write some code for that. Write a URLHandler which gives a socket wrapper, that does buffering. Implement reset methods() as you like.

Thanks, Ramesh



来源:https://stackoverflow.com/questions/5807340/how-to-reset-urlconnection-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!