如何执行 HTTP 删除?我所得到的只是网址

发布于 2024-12-22 14:22:34 字数 98 浏览 2 评论 0原文

我只是 java 的初学者,想知道如何对 URL 进行 HTTP 删除调用。 任何一小段代码或参考资料都会非常有帮助。

我知道这个问题听起来很简单,但我急需这些信息。

I'm just a starter in java and would like to know how to make a HTTP Delete call to a URL.
Any small piece of code or reference material would be very helpful.

I know that the question would sound very simply, but I am in urgent of this information.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

堇年纸鸢 2024-12-29 14:22:34

DELETE、PUT、OPTIONS 方法受到大多数服务器的限制。这是关于这个话题的很好的讨论。
PUT、DELETE、HEAD、大多数网络浏览器都提供等方法?

DELETE, PUT, OPTIONS methods are restricted by most of the servers. Here is good discussion about this topic in SO.
Are the PUT, DELETE, HEAD, etc methods available in most web browsers?

Hello爱情风 2024-12-29 14:22:34

实际上发送 HttpDelete 与 HttpGet 类似,首先您将构建带有所有参数的 url,然后执行请求,以下代码经过测试。

    StringBuilder urlBuilder = new StringBuilder(Config.SERVER)
            .append("/api/deleteInfo?").append("id=")
            .append(id);
    urlBuilder.append("&people=").append(people).toString();

    try {

        HttpClient httpClient = new DefaultHttpClient();
        HttpDelete httpDelete = new HttpDelete(urlBuilder.toString());
        HttpResponse httpResponse = httpClient.execute(httpDelete);
        HttpEntity entity = httpResponse.getEntity();
        final String response = EntityUtils.toString(entity);
        Log.d(TAG, "content = " + response);
    } catch (final Exception e) {
        e.printStackTrace();
        Log.d(TAG, "content = " + e.getMessage());
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
        System.gc();
    }

Actually sending HttpDelete is similar to HttpGet, first you will build the url with all parameters and then just execute the request, the following code is tested.

    StringBuilder urlBuilder = new StringBuilder(Config.SERVER)
            .append("/api/deleteInfo?").append("id=")
            .append(id);
    urlBuilder.append("&people=").append(people).toString();

    try {

        HttpClient httpClient = new DefaultHttpClient();
        HttpDelete httpDelete = new HttpDelete(urlBuilder.toString());
        HttpResponse httpResponse = httpClient.execute(httpDelete);
        HttpEntity entity = httpResponse.getEntity();
        final String response = EntityUtils.toString(entity);
        Log.d(TAG, "content = " + response);
    } catch (final Exception e) {
        e.printStackTrace();
        Log.d(TAG, "content = " + e.getMessage());
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
        System.gc();
    }
手心的温暖 2024-12-29 14:22:34

您可以使用 Restlet。它是一个很好的客户端API。或者你可以执行以下操作

URL url = new URL("http://www.example.com/resource");
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setDoOutput(true);
httpCon.setRequestProperty(
    "Content-Type", "application/x-www-form-urlencoded" );
httpCon.setRequestMethod("DELETE");
httpCon.connect();
httpCon.getInputStream()

You can use Restlet. Its a good client API.Or you can do as following

URL url = new URL("http://www.example.com/resource");
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setDoOutput(true);
httpCon.setRequestProperty(
    "Content-Type", "application/x-www-form-urlencoded" );
httpCon.setRequestMethod("DELETE");
httpCon.connect();
httpCon.getInputStream()
水晶透心 2024-12-29 14:22:34

我想你可以这样打电话:

URL url = new URL("http://www.abcd.com/blog");
HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
httpConnection.setDoOutput(true);
httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded" );
httpConnection.setRequestMethod("DELETE");
httpConnection.connect();

I guess you can call like this :

URL url = new URL("http://www.abcd.com/blog");
HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
httpConnection.setDoOutput(true);
httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded" );
httpConnection.setRequestMethod("DELETE");
httpConnection.connect();
妖妓 2024-12-29 14:22:34

您还可以尝试 Apache HttpClient,它为所有 HTTP 方法(GET、PUT、DELETE、POST、OPTIONS、HEAD 和 TRACE)提供 API。

有关示例,请参见此处: http://hc.apache.org/httpclient-3 .x/methods/delete
API参考在这里:http://hc.apache.org/httpclient- 3.x/apidocs/index.html

干杯

You can also try the Apache HttpClient, it provides an API for all HTTP methods (GET, PUT, DELETE, POST, OPTIONS, HEAD, and TRACE).

For a sample look here: http://hc.apache.org/httpclient-3.x/methods/delete.
API reference is here: http://hc.apache.org/httpclient-3.x/apidocs/index.html

Cheers

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文