I'm trying to download an xml file to parse from a server, but the server doesn't send HTTP headers, just the xml text. I have tried almost every method of downloading files with a URL that I can find, yet everything gives me an I/O Exception. I can load the file into a WebView, but I can't download the file myself.
How can I open a connection and download the xml file with no HTTP headers?
EDIT: Still giving me an I/O Exception, here is the code I'm using:
private class UpdateTask extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void...voids ) {
String url = "http://192.168.1.92/state.xml";
try{
//get XML Pull parser
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
//factory.setValidating(false);
XmlPullParser xpp = factory.newPullParser();
HttpGet httpGet = new HttpGet(url);
DefaultHttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(httpGet);
if(response == null) return null;
HttpEntity httpEntity = response.getEntity();
InputStream inStream = null;
StringBuilder sb = null;
if (httpEntity == null) return null;
try {
inStream = httpEntity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(inStream, "UTF-8"));
sb = new StringBuilder();
String line = null;
while((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
inStream.close();
} catch (Exception ex) {
}
String xmlString = sb.toString();
xpp.setInput(new StringReader(xmlString));
int eventType = xpp.getEventType();
String name = null;
String text = null;
while (eventType != XmlPullParser.END_DOCUMENT) {
if(eventType == XmlPullParser.START_TAG) { // <name>
name = xpp.getName();
System.out.println("got name: " + name);
} else if(eventType == XmlPullParser.END_TAG) {// </name> (xpp.getName();
} else if(eventType == XmlPullParser.TEXT) { // <>text</>
text = xpp.getText();
System.out.println("got text: " + text);
}
eventType = xpp.next();
}
finished = true;
}catch(MalformedURLException e){
System.out.println("Malformed URL");
}catch(IOException e){
System.out.println("IO Exception updating " + name + " at address: " + address);
}catch(XmlPullParserException e){
System.out.println("XML Pull Parser Error updating " + name + " at address: " + address);
}catch(Exception e){
}
return null;
}
}
Gives me an I/O Exception every time. However, if I point it to a server that provides an HTTP header, it downloads just fine. The servers are devices that we make in-house, and the older units didn't provide HTTP headers when you request the state.xml file, so I need to be able to work with both.
I had to open my own socket, create an HTTP Get request, and then remove the HTTP headers for servers that DO have headers.
public String getData(){
String returnData = "";
String requestmsg = "GET /state.xml HTTP/1.0\r\n";
requestmsg += "Connection: close\r\n";
if(cpPassword != null)
requestmsg += "Authorization: Basic " + Base64.encodeBytes(("admin:" + cpPassword).getBytes()) + "\r\n";
requestmsg += "\r\n";
int intPort = 80;
DataOutputStream dos = null;
BufferedReader dis = null;
Socket socket = null;
try {
socket = new Socket(address, intPort);
String data = "";
socket.setSoTimeout(3000); //timeout after 2 seconds
dos = new DataOutputStream(socket.getOutputStream());
dis = new BufferedReader(new InputStreamReader(socket.getInputStream()));
dos.write(requestmsg.getBytes());
StringBuilder sb = new StringBuilder();
while ((data = dis.readLine()) != null) {
sb.append(data);
}
returnData = sb.toString();
if(returnData.length() == 0)
return "";
int xmlIndex = returnData.indexOf( "<?xml" );
returnData = returnData.substring(xmlIndex);
return returnData;
} catch (IOException e) {
Log.e("ClientActivity", "C: Error Getting Data From Socket", e);
returnData = "";
}finally{
try{
if(socket!=null)
socket.close();
}catch(IOException e){
Log.e("ClientActivity", "C: Error Closing Socket", e);
}
}
return returnData;
}