Why does android HttpURLConnection cache input stream results?

I am trying to get an XML file, but it seems to be cached. there is my code:

URL url = new URL("http://delibere.asl3.liguria.it/SVILUPPO/elenco_xml.asp?rand=" + new Random().nextInt()+"&Oggetto=" + text +"&TipoDocumento="+tipoDocumento);
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
urlConn.setDefaultUseCaches(false); 
urlConn.setAllowUserInteraction(true);
urlConn.setDoInput(true);
urlConn.setDoOutput(true);   
urlConn.setUseCaches(false);
urlConn.setRequestMethod("GET");
urlConn.setRequestProperty("Pragma", "no-cache");
urlConn.setRequestProperty("Cache-Control", "no-cache");
urlConn.setRequestProperty("Expires", "-1");
urlConn.setRequestProperty("Content-type", "text/xml");     
urlConn.setRequestProperty("Connection","Keep-Alive"); 
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputStream is = new BufferedInputStream(url.openStream());
Document doc = db.parse(is);
doc.getDocumentElement().normalize();

thank!

+3
source share
2 answers

You understand that you are not using yours HttpURLConnection, right? If you want to get InputStreamhelp HttpURLConnection, you need to call

InputStream is = new BufferedInputStream(urlConn.getInputStream());

In addition, I believe that it is standard to use Apache HttpClient for this kind of thing with Android, since it is built-in and a much better API than standard Java material.

+2
source

It looks like they have a solution with HttpClient that works. You can try, and HttpClient gives a little more flexibility with other problems.

0

Source: https://habr.com/ru/post/1793876/


All Articles