我有一个应用程序,目前我只是通过POST请求向Flask服务器发送一些虚拟数据。烧瓶服务器没有看到帖子数据。当我将它发送到sinatra服务器时,后期数据没有问题。
Android代码:
URL url = new URL(imageRequests[0].getUrl());
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
String urlParameters = "param1=a¶m2=b¶m3=c";
System.out.println(urlParameters);
urlConnection.setDoOutput(true);
urlConnection.setChunkedStreamingMode(0);
DataOutputStream wr = new DataOutputStream(urlConnection.getOutputStream ());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
烧瓶代码:
@app.route('/', methods=['POST'])
def submit():
print request.form
return "Uploaded"
Flask输出:
Loading from savePath test.tree
* Running on http://0.0.0.0:5000/
ImmutableMultiDict([])
10.100.85.69 - - [25/Jul/2014 17:09:07] "POST / HTTP/1.1" 200 -
Sinatra代码:
post '/' do
puts params
"Uploaded"
end
Sinatra输出:
== Sinatra/1.4.5 has taken the stage on 4567 for development with backup from WEBrick
[2014-07-25 17:07:27] INFO WEBrick::HTTPServer#start: pid=18674 port=4567
{"param1"=>"a", "param2"=>"b", "param3"=>"c"}
10.100.85.69 - - [25/Jul/2014 17:07:35] "POST / HTTP/1.1" 200 12 0.0053
10.100.85.69 - - [25/Jul/2014:17:07:34 BST] "POST / HTTP/1.1" 200 12
- -> /
我真的很困惑为什么Sinatra获得了帖子数据,但烧瓶并不是。我唯一的猜测是来自android的POST请求不太正确,而sinatra对此更加宽容。
是这样的吗?
编辑:netcat通过端口监听的输出
POST / HTTP/1.1
User-Agent: Dalvik/1.6.0 (Linux; U; Android 4.4.4; Nexus 7 Build/KTU84P)
Host: 10.100.85.210:5000
Connection: Keep-Alive
Accept-Encoding: gzip
Content-Type: application/x-www-form-urlencoded
Transfer-Encoding: chunked
1b
?param1=a¶m2=b¶m3=c
0
答案 0 :(得分:0)
将其更改为此。这似乎是分块输出的一个问题
URL url = new URL(imageRequests[0].getUrl());
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
String urlParameters = "param1=value¶m2=value";
System.out.println(urlParameters);
urlConnection.setDoOutput(true);
urlConnection.setRequestProperty("Content-Length", String.valueOf(urlParameters.getBytes().length));
urlConnection.setRequestMethod("POST");
OutputStream os = urlConnection.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(urlParameters);
writer.close();