繁体   English   中英

Android java 版本的 Python 项目:错误 400:错误请求

[英]Android java version of a Python project: Error 400: Bad request

我正在尝试将 Python 中的松下远程应用程序移植到 Android

Android版本:
https://github.com/GreenSnakeLinux/Panasonic
Python 版本:
https://github.com/florianholzapfel/panasonic-viera

不幸的是,Android 版本在 HttpURLConnection 上回答错误 400(错误请求)我猜 header 与 Python 不完全一样,但找不到什么是错误的版本

这是 Python 项目的摘录和我在 Android 中的等效项目:

Python:

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()

Android:

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());

谢谢你的帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM