我正在为我的网络应用程序使用第三方的网络服务。 为了更新实体,他们提供了 MERGE 方法(他们用于新实体的 POST 方法(它需要所有字段)),所以我搜索了此方法是否有任何示例提供,但没有成功。 有人可以告诉我如何实现这一目标吗? 有人也可以告诉我如何使用 PATCH 方法吗?
提前致谢。
最佳答案
HttpURLConnection.setRequestMethod
不支持 MERGE
和 PATCH
方法。
这是documentation对于HttpURLConnection.setRequestMethod
:
Set the method for the URL request, one of: GET POST HEAD OPTIONS PUT DELETE TRACE are legal, subject to protocol restrictions. The default method is GET.
您的第三方 Web 服务是 OData 服务吗? MERGE
方法是为 OData 发明的,官方 HTTP 协议(protocol)中不存在。这是relevant documentation来自 Microsoft(OData 的发明者):
The remainder of this section defines a custom HTTP MERGE method that is used in the OData 1.0 and OData 2.0 protocols to specify that an update is to be completed by using merge semantics... The only difference between an HTTP request that uses MERGE or PATCH and PUT is client intent... Since MERGE is not one of the verbs that is defined in the HTTP specification [RFC2616], using the MERGE verb might not flow through network intermediaries as seamlessly as methods that are defined in the HTTP specification...
作为解决方案,我建议将您的有效负载发送到 OData batch request 。这是一个特殊的 POST
请求,其负载可以包含多个 HTTP 请求。您可以在 batch
负载中指定 MERGE
和 PATCH
请求。
关于java - 如何使用 HttpURLConnection 或 HttpClient 库在 java 中进行 MERGE 调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14373007/