Android的HttpURLConnection的POST [英] Android HTTPUrlConnection POST

查看:177
本文介绍了Android的HttpURLConnection的POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用HttpURLConnection类将数据发送到通过POST的服务器。我设置,则头得到的输出流和写入数据的5个字节(M = 005),然后关闭输出流。

在我收到的所有头,正确的内容长度,但然后我得到一个零长度线和服务器上的readLine挂起服务器。

什么似乎发生在客户端接近从未实际发生所以整个数据不会写入因此,服务器永远不会得到它。

我看过无数的例子,并尝试了各种变化,看看我能以任何方式影响所有这一切不到理想的效果。例如关闭保持有效指示,迫使CRLF在然后结束我的数据(这迫使我从数据在服务器上,但连接仍然不会关闭。(这只是用于测试),试图打印的作家。

由于许多例子都在做我什么,我以为这是简单的东西,我俯瞰,但我就是不能看到它。任何帮助将是AP preciated。

 的StringBuilder postDataBuilder.append(M =)追加(URLEn coder.en code(005,UTF8))。
    字节[] POSTDATA = NULL;
    POSTDATA = postDataBuilder.toString()的getBytes()。
    URL =新的URL(HTTP://+ serverAddress +:+将String.valueOf(serverPort));
    康恩=(HttpURLConnection类)url.openConnection();    conn.setDoOutput(真);
    conn.setRequestMethod(POST);
    conn.setRequestProperty(内容长度,Integer.toString(postData.length));
    conn.setUseCaches(假);    OutputStream的OUT = conn.getOutputStream();
    out.write(POSTDATA);
    out.close();    INT响应code = conn.getResponse code();    //执行上述行后,服务器启动成功readlines方法
    //直到它到达后数据的服务器挂起时。如果我重新启动
    //客户端然后将数据终于得到通过,但在连接
    //服务器端永远不会结束。


解决方案

哎呀。在我们的服务器端错误,在做一个替代的readLine性质的阅读。由于没有CRLF就会挂起。

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. 

解决方案

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

这篇关于Android的HttpURLConnection的POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆