Android HTTPUrlConnection POST

女生的网名这么多〃 提交于 2019-12-10 18:44:51

问题


I am using HttpURLConnection to send data to a server via POST. I set the headers then get the output stream and write 5 bytes of data ("M=005") then close the output stream.

On the server I receive all the headers,the correct content length but then I get a zero length line and the server hangs on readLine.

What appears to happen is the client close never actually happens so the entire data is not written hence the server never gets it.

I have read numerous examples and tried various changes to see if I can effect this in any way all with less than desirable results. For example turning off keep alives, forcing a CRLF at then end of my data (which forces my data out at the server but the connection still doesn't close. (This was just for testing), trying a print writer.

Since many examples are doing what I am I assume it's something simple I am overlooking but I just can't see it. Any help would be appreciated.

    StringBuilder postDataBuilder.append("M=").append(URLEncoder.encode("005", UTF8));
    byte[] postData = null;
    postData = postDataBuilder.toString().getBytes();


    url = new URL("http://" + serverAddress + ":" + String.valueOf(serverPort));
    conn = (HttpURLConnection) url.openConnection();

    conn.setDoOutput(true);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Length", Integer.toString(postData.length));
    conn.setUseCaches(false);

    OutputStream out = conn.getOutputStream();
    out.write(postData);
    out.close();

    int responseCode = conn.getResponseCode();

    // After executing the above line the server starts to successfully readLines
    // until it gets to the post data when the server hangs.  If I restart the
    // client side then the data finally gets through but the connection on the
    // server side never ends. 

回答1:


Ooops. Bug on our server side that was doing a readLine instead of character read. Since there is no CRLF it will hang.



来源:https://stackoverflow.com/questions/10759782/android-httpurlconnection-post

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!