My WiFi Samsung air conditioner doesn't respond in a standard XML format. Is there a way to maybe ignore the first response?
If I open a SSL connection it responds with two lines:
DRC-1.00
<?xml version="1.0" encoding="utf-8" ?><Update Type="InvalidateAccount"/>
To which I have to respond with:
<?xml version="1.0" encoding="utf-8" ?><Request Type="GetToken" />
The stack trace in Android is:
W/System.err﹕ java.net.ProtocolException: Unexpected status line: DRC-1.00
My code:
URL url;
URLConnection conn = null;
try {
//Create connection
url = new URL(targetURL);
// for not trusted ssl connections (https)
FakeX509TrustManager.allowAllSSL();
Log.v(TAG, "Set \"javax.net.debug\" to \"all\"");
System.setProperty("javax.net.debug", "all");
conn = url.openConnection();
conn.setReadTimeout(5000 /* milliseconds */);
conn.setConnectTimeout(5000 /* milliseconds */);
conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
//Send request
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.flush();
writer.close();
os.close();
Log.i(TAG, "Response " + conn.toString());
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
XMLReader xmlReader = parser.getXMLReader();
xmlReader.parse(new InputSource(conn.getInputStream())); // Error here
Log.v(TAG, xmlReader.toString());
} catch (IOException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
}
Ended up with:
try {
// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
@Override
public void checkClientTrusted(java.security.cert.X509Certificate[]
chain, String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(java.security.cert.X509Certificate[]
chain, String authType) throws CertificateException {
}
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
}
};
// Install the all-trusting trust manager
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
SSLSocketFactory sslsocketfactory = sc.getSocketFactory();
sslsocket = (SSLSocket) sslsocketfactory.createSocket(targetIp, port);
sslsocket.setKeepAlive(true);
// Sets this socket's read timeout in milliseconds.
// sslsocket.setSoTimeout(timeoutSocket);
Log.d(TAG, "Sending: " + xmlAction + xmlValue + XmlConstants.NEW_LINE);
String line;
ArrayList<String> receivedLines = new ArrayList<String>();
DataOutputStream outToServerSSL = new DataOutputStream(sslsocket.getOutputStream());
BufferedReader inFromServerSSL = new BufferedReader(new InputStreamReader(sslsocket.getInputStream()));
outToServerSSL.writeBytes(xmlAction + xmlValue + XmlConstants.NEW_LINE + XmlConstants.NEW_LINE);
while ((line = inFromServerSSL.readLine()) != null) {
receivedLines.add(line);
Log.d(TAG, "Received: " + line);
}
Log.d(TAG, "Closing tha sizzle");
inFromServerSSL.close();
outToServerSSL.close();
return ParseXmlForStatusCode(receivedLines);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (sslsocket != null) {
try {
sslsocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}