2

I have a short Android-Java client program which sends a basic information to bottle-python server with POST method. In the first version of code, server does not show anything. However, In the second version it works but I cannot understand what this additional line do because it has anything to do with posting content. I really appreciate if someone helps me figure this out.(There is nothing wrong with the server code since I can properly send request with python requests and my browsers).

This is the first version of client code:

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

Second version:

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

Bottle(python) server:

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

With first client code server shows nothing.

With second code server shows:

it works
hey
192.168.1.24 - - [31/Dec/2018 17:10:28] "POST / HTTP/1.1" 200 3

Which is as I expected.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

3 Answers3

1

With your first code snippet the output stream is still open. So the server does not know if it got the complete request. Probably just closing the stream would work as well.

However, I would make at least a call to getResponseCode to see the outcome of the request.

Henry
  • 42,982
  • 7
  • 68
  • 84
  • I added "myPusher.close(); os.close();" to first code snippet. It did not work either. Might the server understand the end of POST when Inputstream is opened ? – Serkan Göktaş Dec 31 '18 at 14:41
  • I was not sure if a close is enough as I always check at least the response code to get some feedback on the outcome. Requesting the input stream is a clear sign that you are done with writing data, and the connection will signal this to the server. But if you do this without closing, some data may be lost if you use a buffered stream. – Henry Dec 31 '18 at 14:57
  • I guess, as you say, server waits for Inputstream to open in order to assert that post request is gotten. However it is interesting and I could not find any explanations of this situation in APIs. Thank you for your help. – Serkan Göktaş Dec 31 '18 at 23:01
1

Your java code seems incomplete for sending a post request. I think by using this code, you can make it work for yourself.

Akram
  • 2,158
  • 1
  • 17
  • 24
1

The PrintStream is a buffered type, this means you should add a flush operation after each print(), or use println() instead.

ucMedia
  • 4,105
  • 4
  • 38
  • 46