【问题标题】:Can't read form data of http post request in java无法在java中读取http post请求的表单数据
【发布时间】:2014-09-08 22:28:38
【问题描述】:

程序只是读取表格头部并停止。

//...

Socket client = server.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));

System.out.println("Request:");

while ((line = in.readLine()) != null)
    if (line.length() > 0)
    System.out.println("\t" + line);
        else break;

in.close();
client.close();

当我尝试在浏览器上提交一些表单时:

等待请求... 已接受连接。 要求: POST /form.php HTTP/1.1 主持人:x-x-x-x 连接:保持活动 内容长度:31 缓存控制:max-age=0 接受:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 来源:http://neviat.us.to 用户代理:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36 内容类型:application/x-www-form-urlencoded 参考:http://x-x-x-x 接受编码:gzip、deflate、sdch 接受语言:pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4 连接关闭。

我收到了Content-Length,但对内容本身一无所知。 ¬¬ 我认为那可能是else break;,所以我把它取下来又试了一次:

等待请求... 已接受连接。 要求: POST /buy.php HTTP/1.1 主持人:x-x-x-x 连接:保持活动 内容长度:31 缓存控制:max-age=0 接受:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 来源:http://neviat.us.to 用户代理:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36 内容类型:application/x-www-form-urlencoded 参考:http://x-x-x-x 接受编码:gzip、deflate、sdch 接受语言:pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4 // 停在这里,等待流...

但内容也不存在。

我怎样才能得到它?

【问题讨论】:

    标签: java http post httprequest


    【解决方案1】:

    变化:

    if (line.length() > 0)
    

    if (line.length() > -1)
    

    长度为 0 的行完全有效

    【讨论】:

    • 但是即使我没有检查并打印所有我得到的内容,也不会打印内容。
    【解决方案2】:

    更改代码:

    //...

    Socket client = server.accept();
    BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
    
    System.out.println("Request:");
    
    while ((line = in.readLine()) != -1)
        if (line.length() > 0)
        System.out.println("\t" + line);
            else break;
    
    in.close();
    client.close();
    

    【讨论】:

      猜你喜欢
      • 2021-12-04
      • 1970-01-01
      • 2012-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-23
      • 1970-01-01
      相关资源
      最近更新 更多