Python Request et Android HttpURLConnection équivalent

Python Request et Android HttpURLConnection équivalent - Android - Programmation

Marsh Posté le 14-11-2019 à 11:13:04    

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 :
  1. headers = {
  2.     'Host': '{}:{}'.format(self._host, self._port),
  3.     'Content-Length': len(soap_body),
  4.     'Content-Type': 'text/xml; charset=utf-8',
  5.     'SOAPAction': '"urn:{}#{}"'.format(urn, action),
  6. }
  7. url = 'http://{}:{}/{}'.format(self._host, self._port, url)
  8. req = Request(url, soap_body, headers)
  9. res = urlopen(req, timeout=5).read()


 
Code Android:

Code :
  1. urlConnection = (HttpURLConnection) url.openConnection();
  2. urlConnection.setRequestMethod("POST" );
  3. urlConnection.setDoOutput(true);
  4. urlConnection.setConnectTimeout(3000);
  5. urlConnection.setReadTimeout(5000);
  6. urlConnection.setRequestProperty("Host", _host + ":" + _port);
  7. urlConnection.setRequestProperty("Content-Length", Integer.toString(soap_body.length()));
  8. urlConnection.setRequestProperty("Content-Type", "text/xml; charset=utf-8" );
  9. urlConnection.setRequestProperty("SOAPAction", "\"urn:" + urn + "#" + action + "\"" );
  10. urlConnection.setChunkedStreamingMode(0);
  11. OutputStream send = urlConnection.getOutputStream();
  12. BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(send, "UTF-8" ));
  13. writer.write(soap_body);
  14. writer.flush();
  15. writer.close();
  16. send.close();
  17. int responseCode = urlConnection.getResponseCode();
  18. if(responseCode == HttpURLConnection.HTTP_OK)
  19.     server_response = readStream(urlConnection.getInputStream());


 
Merci de votre aide

Reply

Marsh Posté le 14-11-2019 à 11:13:04   

Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed