J2ME中的HTTP删除请求



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

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

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();

试试看。所有最好的

相关内容

  • 没有找到相关文章

最新更新