HttpDelete 是否包含像 setEntity()、HttpPost 或 HttpPut 这样的方法?当我使用 HttpPost 时,我会执行以下操作:
httppost.setEntity(new UrlEncodedFormEntity(
getNameValuePairsForFriends(context, friendID)));
如何使用删除来做到这一点?
我不相信 HTTP DELETE 需要输入 - 我相信它就像一个 GET 变体。
HTTP Client 提供的实现似乎也支持这个猜想。
如果您希望提供带有正文的删除,您/可能/想要考虑使用 POST 到接受正文的位置。
但是在回答您的问题时,不,删除不接受正文。您可以添加查询参数,但不能添加正文。
class MyDelete extends HttpPost
{
public MyDelete(String url){
super(url);
}
@Override
public String getMethod() {
return "DELETE";
}
}
让您的班级在那里扩展 http delete 班级,并在发送实体的班级对象期间,您将能够在 httpdelete 中发布数据
HttpResponse httpResponse;
String result = null;
HttpClient httpClient = new DefaultHttpClient();
HttpConnectionParams
.setConnectionTimeout(httpClient.getParams(), 10000);
MyDelete httpDelete = new MyDelete(urlUnfollowPatientBundle);
StringEntity entity = null;
try {
entity = new StringEntity(rawData);
httpDelete.setEntity(entity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
try {
httpResponse = httpClient.execute(httpDelete);
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity httpEntity = httpResponse.getEntity();
result = EntityUtils.toString(httpEntity);
status = true;
}
HTTPDelete 不会携带任何有效负载。
HttpDelete 只会删除要删除的 uri/url 并向所述资源发出 DELETE HTTP Header。
在 Scala 中,以下代码适用于我通过 HTTP DELETE 发送正文
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase
import java.net.URI
class HttpDeleteWithBody(uri: URI) extends HttpEntityEnclosingRequestBase {
val METHOD_NAME: String = "DELETE"
def getMethod: String = METHOD_NAME
super.setURI(uri)
setURI(uri)
}
试试这个,只需扩展 HttpDelete:
类 HttpDeleteWithBody 扩展 HttpDelete {
private HttpEntity entity;
public HttpDeleteWithBody(String url) {
super(url);
}
public HttpEntity getEntity() {
return this.entity;
}
public void setEntity(final HttpEntity entity) {
this.entity = entity;
}
}