HttpDelete中的setEntity

时间:2013-01-28 11:29:19

标签: java android

HttpDelete是否包含类似setEntity()的方法,如HttpPost或HttpPut? 当我使用HttpPost时,我会做这样的事情:

httppost.setEntity(new UrlEncodedFormEntity(
    getNameValuePairsForFriends(context, friendID))); 

如何通过删除执行此操作?

5 个答案:

答案 0 :(得分:4)

我不相信HTTP DELETE接受输入 - 我相信它就像一个GET变种。

HTTP Client提供的实现似乎也支持这个猜想。

如果您希望使用正文提供删除,您/可能/想要考虑将POST用于接受正文的位置。

但是在回答你的问题时,不,删除不接受一个正文。您可以添加查询参数,但不能添加正文。

答案 1 :(得分:2)

HTTPDelete不会携带任何有效负载。

HttpDelete将删除uri / url并向所述资源发出DELETE HTTP Header。

答案 2 :(得分:1)

class MyDelete extends HttpPost
{
    public MyDelete(String url){
        super(url);
    }
    @Override
    public String getMethod() {
        return "DELETE";
    }
}

让你的类在那里扩展http删除类,并在使类的对象发送实体时,你将能够在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;
    }

答案 3 :(得分:0)

尝试一下,只需扩展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;
}

}

答案 4 :(得分:0)

在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)
}