带正文的 HttpDelete
我正在尝试使用 HttpDelete 对象来调用 Web 服务的删除方法。 Web 服务的代码从消息正文中解析 JSON。但是,我无法理解如何将主体添加到 HttpDelete 对象。有办法做到这一点吗?
使用 HttpPut 和 HttpPost,我调用 setEntity 方法并传入我的 JSON。 HttpDelete 似乎没有任何此类方法。
如果无法为 HttpDelete 对象设置主体,您能否将我链接到使用 HttpDelete 超类的资源,以便我可以设置方法(删除)并设置主体。我知道这并不理想,但目前我无法更改网络服务。
I'm attempting to use an HttpDelete object to invoke a web service's delete method. The web service's code parses JSON from the message's body. However, I'm failing to understand how to add a body to an HttpDelete object. Is there a way to do this?
With HttpPut and HttpPost, I call the setEntity method and pass in my JSON. There doesn't appear to be any such method for HttpDelete.
If there is no way to set a body for an HttpDelete object, could you please link me to a resource that uses a super class of HttpDelete such that I can set the method (delete) and set a body. I know that isn't ideal, but at this point I can't alter the web service.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您是否尝试过按如下方式覆盖
HttpEntityEnlookingRequestBase
:这将创建一个具有
setEntity
方法的类似HttpDelete
的对象。我认为抽象类几乎可以为你做所有的事情,所以这可能就是你所需要的。FWIW,代码基于 此源到 Google 出现的 HttpPost。
Have you tried overriding
HttpEntityEnclosingRequestBase
as follows:That will create a
HttpDelete
-lookalike that has asetEntity
method. I think the abstract class does almost everything for you, so that may be all that's needed.FWIW, the code is based on this source to HttpPost that Google turned up.
按照 Walter Mudnt 的建议,您可以使用此代码。它有效,只是在测试我的 REST Web 服务时完成的。
要访问响应,您只需执行以下操作:
response.getStatusLine();
Following Walter Mudnt advice, you can use this code. It works, just made it while testing my REST webservice.
To access the response you can simply do:
response.getStatusLine();
对于
HTTP DELETE
请求中是否允许正文的问题有不同的解释。例如,请参阅此。在HTTP 1.1规范中没有明确禁止。在我看来,您不应该在HTTP DELETE
中使用body。尽管如此,我认为您应该使用像
mysite/myobject/objectId
(shop.com/order/1234
) 这样的 URL,其中objectId
(网址的一部分)是附加信息。作为替代方案,您可以使用URL参数:mysite/myobject?objectName=table&color=red
在HTTP DELETE 请求。以“?”开头的部分是 urlencoded 参数分隔为 '&'。
如果您想发送更复杂的信息,您可以将数据转换为 JSON DataContractJsonSerializer 或 JavaScriptSerializer ,然后发送转换后的数据 (我稍后将其命名为 myJsonData 的字符串)也作为参数:mysite/myobject?objectInfo=myJsonData。
如果您需要在 HTTP DELETE 请求中发送太多附加数据,从而导致 URL 长度出现问题,那么您最好更改应用程序的设计。
更新:如果您确实想为每个 HTTP DELETE 发送一些正文,您可以执行此操作,例如遵循
完整代码可能会稍微复杂一些,但这个已经可以工作了。尽管如此,我仍然说 Web 服务需要 HTTP DELETE 请求正文中的数据设计得很糟糕。
There are different interpretation in the question whether the body is allowed or not in the
HTTP DELETE
request. See this for example. In the HTTP 1.1 specification it is not explicitly prohibied. In my opinion you should not use body in theHTTP DELETE
.Nevertherless I think that you should use URL like
mysite/myobject/objectId
(shop.com/order/1234
) where theobjectId
(a part of the url) is the additional information. As an alternative you can use URL parameters:mysite/myobject?objectName=table&color=red
to send additipnal information to the server in theHTTP DELETE
request. The part starting with '?' is the urlencoded parameters devided dy '&'.If you want to send more complex information you can convert the data to JSON with respect of DataContractJsonSerializer or JavaScriptSerializer and then send the converted data (a string which I name
myJsonData
later) also as the parameter:mysite/myobject?objectInfo=myJsonData
.If you need to send too much additionnal data as a part of
HTTP DELETE
request so that you have problem with the URL length then you should probably better change the design of your application.UPDATED: Iy you do want send some body per HTTP DELETE you can do this for example like following
the full code could be a little more complex, but this one already will work. Nevertheless I continue to say that Web Service needed data in the body of HTTP DELETE request is bad designed.
用这个,
use this,
在改造中,
您必须通过某种方式触发条件,并且可能必须对 url/header/body 进行一些过滤以删除触发器,
除非删除 url/body/header 足够独特,不会与 post 或 get 请求发生冲突。
in retrofit
you have to trigger condition, via something and potentially have to do some filtering for the url/header/body to remove the trigger,
unless the delete url/body/header is unique enough to not collide with post or get requests.