kurdt2002 | Bonjour,
J'essaye de porter sur Android une application Python de télécommande de télé Panasonic:
Android version:
https://github.com/GreenSnakeLinux/Panasonic
Python version:
https://github.com/florianholzapfel/panasonic-viera
Mais j'ai une erreur code 400 (Bad request) sur HttpURLConnection
Il doit y avoir des différences avec Python mais je ne sais pas lesquels, peut-être coté header
Voici un extrait du code Python et ma version Android:
Code Python:
Code :
- headers = {
- 'Host': '{}:{}'.format(self._host, self._port),
- 'Content-Length': len(soap_body),
- 'Content-Type': 'text/xml; charset=utf-8',
- 'SOAPAction': '"urn:{}#{}"'.format(urn, action),
- }
- url = 'http://{}:{}/{}'.format(self._host, self._port, url)
- req = Request(url, soap_body, headers)
- res = urlopen(req, timeout=5).read()
|
Code Android:
Code :
- urlConnection = (HttpURLConnection) url.openConnection();
- urlConnection.setRequestMethod("POST" );
- urlConnection.setDoOutput(true);
- urlConnection.setConnectTimeout(3000);
- urlConnection.setReadTimeout(5000);
- urlConnection.setRequestProperty("Host", _host + ":" + _port);
- urlConnection.setRequestProperty("Content-Length", Integer.toString(soap_body.length()));
- urlConnection.setRequestProperty("Content-Type", "text/xml; charset=utf-8" );
- urlConnection.setRequestProperty("SOAPAction", "\"urn:" + urn + "#" + action + "\"" );
- urlConnection.setChunkedStreamingMode(0);
- OutputStream send = urlConnection.getOutputStream();
- BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(send, "UTF-8" ));
- writer.write(soap_body);
- writer.flush();
- writer.close();
- send.close();
- int responseCode = urlConnection.getResponseCode();
- if(responseCode == HttpURLConnection.HTTP_OK)
- server_response = readStream(urlConnection.getInputStream());
|
Merci de votre aide |