gpt4 book ai didi

android - 删除带正文的请求(不使用已弃用的 HttpEntity)

转载 作者:太空狗 更新时间:2023-10-29 14:05:10 26 4
gpt4 key购买 nike

我想执行带有主体的 HTTP DELETE 请求。我正在使用 Spring 框架。

为此,我必须进行以下修改:

RestTemplate _deleteManager;
_deleteManager = new RestTemplate();
_deleteManager.setRequestFactory(new HttpComponentsClientHttpRequestFactory() {
protected HttpUriRequest createHttpRequest(HttpMethod httpMethod, URI uri) {
if (HttpMethod.DELETE == httpMethod) {
return new HttpEntityEnclosingDeleteRequest(uri);
}
return super.createHttpRequest(httpMethod, uri);
}
});

public static class HttpEntityEnclosingDeleteRequest extends HttpEntityEnclosingRequestBase {
public HttpEntityEnclosingDeleteRequest(final URI uri) {
super();
setURI(uri);
}

@Override
public String getMethod() {
return "DELETE";
}
}

然后我使用_deleteManager 进行调用

_deleteManager.exchange(url, HttpMethod.DELETE, requestEntity, ResponseClass.class);

这可以正常工作,但是在代码中 HttpUriRequestHttpEntityEnclosingRequestBase 被标记为已弃用。

我应该改用什么替代方案?我在网上搜索但找不到任何东西。

编辑:或者一个简单的答案来说明如何对正文执行 DELETE 请求就足够了。

谢谢

最佳答案

检查 related topic在 6.0 的官方更新日志中。所以你应该使用 HttpURLConnection类(class)。

一个例子:

URL url = new URL("your_url");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try {
urlConnection.setRequestMethod("DELETE");
urlConnection.setDoOutput(true);
// Maybe you should set the request properties here too, but it depends on your body.

OutputStreamWriter osw = new OutputStreamWriter(urlConnection.getOutputStream());
osw.write("this_is_the_body");
osw.flush();

int HttpResult = urlConnection.getResponseCode();
if(HttpResult == HttpURLConnection.HTTP_OK) {
InputStream is = new BufferedInputStream(urlConnection.getInputStream());
// Handle the response.
} else {
// ...
}
}
finally {
urlConnection.disconnect();
}

关于android - 删除带正文的请求(不使用已弃用的 HttpEntity),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33038954/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com