java - How to read XML returned by HTTP POST request? -


not duplicate of other question.

i sending post request this:

        string urlparameters = "a=b&c=d";         string request = "http://www.example.com/";          url url = new url(request);         httpurlconnection connection = (httpurlconnection) url.openconnection();               connection.setdooutput(true);         connection.setdoinput(true);         connection.setinstancefollowredirects(false);          connection.setrequestmethod("post");         connection.setrequestproperty("content-type", "application/x-www-form-urlencoded");          connection.setrequestproperty("charset", "utf-8");         connection.setrequestproperty("content-length", "" + integer.tostring(urlparameters.getbytes().length));         connection.setusecaches(false);          dataoutputstream wr = new dataoutputstream(connection.getoutputstream());         wr.writebytes(urlparameters);         wr.flush();         wr.close();         connection.disconnect(); 

how can read xml response returned http post request? particularly, want save response file .xml file, , read it. usual get requests, use this:

    saxbuilder builder = new saxbuilder();     url website = new url(urltoparse);     readablebytechannel rbc = channels.newchannel(website.openstream());     fileoutputstream fos = new fileoutputstream("request.xml");     fos.getchannel().transferfrom(rbc, 0, 1 << 24);     fos.close();     // work 

addendum: i'm using following code , works fine. however, neglects spacing , new lines , treats complete xml contents single line. how fix it?

    inputstream = connection.getinputstream();     bufferedreader br = new bufferedreader(new inputstreamreader(is));     stringbuilder sb1 = new stringbuilder();     string line;     while ((line = br.readline()) != null) {         sb1.append(line);     }     fileoutputstream f = new fileoutputstream("request.xml");     f.write(sb1.tostring().getbytes());     f.close();     br.close(); 

don't use readers , readline() xml data. use inputstreams , byte[]s.


Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -