POST 请求未发送
POST request is not sent
我有一个简短的 Android-Java 客户端程序,它使用 POST 方法将基本信息发送到 bottle-python 服务器。在第一个版本的代码中,服务器不显示任何内容。但是,在第二个版本中它可以工作,但我无法理解这一行的作用,因为它与发布内容有任何关系。如果有人帮我解决这个问题,我真的很感激。(服务器代码没有任何问题,因为我可以使用 python 请求和我的浏览器正确发送请求)。
这是第一版客户端代码:
String url = "http://192.168.1.23:8080/";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setDoOutput(true);
OutputStream os = con.getOutputStream();
PrintStream myPusher = new PrintStream(os );
myPusher.print("param1=hey");
第二个版本:
String url = "http://192.168.1.23:8080/";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setDoOutput(true);
OutputStream os = con.getOutputStream();
PrintStream myPusher = new PrintStream(os );
myPusher.print("param1=hey");
InputStream in= con.getInputStream(); //Nothing changed but only this additional line
瓶子(python) 服务器:
@app.route('/', method="POST")
def hello():
print("it works")
name = request.forms.get("param1")
print(name)
return name
@app.route('/')
def hello():
i=0
print("it works")
run(app, host="192.168.1.23", port=8080)
第一个客户端代码服务器什么都不显示。
第二个代码服务器显示:
it works
hey
192.168.1.24 - - [31/Dec/2018 17:10:28] "POST / HTTP/1.1" 200 3
正如我所料。
对于您的第一个代码片段,输出流仍然打开。所以服务器不知道它是否得到了完整的请求。可能只是关闭流也可以。
但是,我至少会调用 getResponseCode
来查看请求的结果。
您的 java 代码似乎不完整,无法发送 post 请求。
我认为通过使用 this code,您可以让它为您所用。
PrintStream是缓冲类型,这意味着你应该在每个print()
之后添加一个刷新操作,或者使用println()
代替。
我有一个简短的 Android-Java 客户端程序,它使用 POST 方法将基本信息发送到 bottle-python 服务器。在第一个版本的代码中,服务器不显示任何内容。但是,在第二个版本中它可以工作,但我无法理解这一行的作用,因为它与发布内容有任何关系。如果有人帮我解决这个问题,我真的很感激。(服务器代码没有任何问题,因为我可以使用 python 请求和我的浏览器正确发送请求)。
这是第一版客户端代码:
String url = "http://192.168.1.23:8080/";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setDoOutput(true);
OutputStream os = con.getOutputStream();
PrintStream myPusher = new PrintStream(os );
myPusher.print("param1=hey");
第二个版本:
String url = "http://192.168.1.23:8080/";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setDoOutput(true);
OutputStream os = con.getOutputStream();
PrintStream myPusher = new PrintStream(os );
myPusher.print("param1=hey");
InputStream in= con.getInputStream(); //Nothing changed but only this additional line
瓶子(python) 服务器:
@app.route('/', method="POST")
def hello():
print("it works")
name = request.forms.get("param1")
print(name)
return name
@app.route('/')
def hello():
i=0
print("it works")
run(app, host="192.168.1.23", port=8080)
第一个客户端代码服务器什么都不显示。
第二个代码服务器显示:
it works
hey
192.168.1.24 - - [31/Dec/2018 17:10:28] "POST / HTTP/1.1" 200 3
正如我所料。
对于您的第一个代码片段,输出流仍然打开。所以服务器不知道它是否得到了完整的请求。可能只是关闭流也可以。
但是,我至少会调用 getResponseCode
来查看请求的结果。
您的 java 代码似乎不完整,无法发送 post 请求。 我认为通过使用 this code,您可以让它为您所用。
PrintStream是缓冲类型,这意味着你应该在每个print()
之后添加一个刷新操作,或者使用println()
代替。