【问题标题】:HTTP delete request in J2MEJ2ME 中的 HTTP 删除请求
【发布时间】:2012-03-01 13:49:38
【问题描述】:

我正在尝试从 J2ME 在 Rails 服务器上发送删除请求

输出是

STATUS: 422 

并且该元素没有被删除。

代码如下:

    HttpConnection hc = null;
    InputStream istrm = null;
    String msg = "_method=DELETE";
    System.out.println(id);
    try {
        hc = (HttpConnection) Connector.open(server + "documents/" + id + ".xml");
        hc.setRequestMethod(HttpConnection.POST);
        hc.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
        hc.setRequestProperty("Content-length", "" + msg.getBytes().length);

        OutputStream out = hc.openOutputStream();
        out.write(msg.getBytes());
        out.close();

        System.out.println("STATUS: " + hc.getResponseCode()+ hc.getResponseMessage());

【问题讨论】:

    标签: http java-me midp


    【解决方案1】:

    在关闭输出流之前添加以下语句

    int res_code=hc.getResponseCode();
    if(res_code==HttpConnection.HTTP_OK)
    {
    //Success
    }
    else
    {
    //Failure
    }
    .........
    //After performing all operations then close streams and connection
    out.close();
    hc.close();
    

    试试吧,一切顺利

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-09
      • 2021-06-23
      • 2018-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-20
      • 2021-09-22
      相关资源
      最近更新 更多