J2ME中的HTTP删除请求

时间:2012-03-01 13:49:38

标签: http java-me midp

我正在尝试从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());

1 个答案:

答案 0 :(得分:0)

在关闭outputstream

之前添加以下语句
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();

试试吧。一切顺利