Enables Httpdelete to send body information in Java
Using the Delete method in the RESTful API, the Android developer's colleague encountered a problem, unable to carry the body information when using Httpdelete to perform the delete operation, and found a solution after a long study. We look at httpclient-4.2.3 source can be found, methods package contains HttpGet, HttpPost, Httpput, Httpdelete and other classes to implement common HTTP operations. Among them, HttpPost inherits from the Httpentityenclosingrequestbase,httpentityenclosingrequestbase class and implements the Httpentityenclosingrequest interface, Implements the method of Setentity. The Httpentity object cannot be set because Httpdelete inherits from Httprequestbase and does not implement the Setentity method. The solution is obvious, and we can implement a Myhttpdelete class ourselves, inheriting from httpentityenclosingrequestbase, overwriting the GetMethod method in which it returns "DELETE".
public class Myhttpdelete extends Httpentityenclosingrequestbase {
public static final String method_name = "DELETE";
Public String GetMethod () {
return method_name;
}
Public Myhttpdelete (final String URI) {
Super ();
Seturi (Uri.create (URI));
}
Public myhttpdelete (final URI Uri) {
Super ();
Seturi (URI);
}
Public Myhttpdelete () {
Super ();
}
}
When you use the Delete method, you can do so directly as follows
Defaulthttpclient httpClient = new Defaulthttpclient ();
Myhttpdelete Delete = new Myhttpdelete ("http://url.com");
list<namevaluepair> namevaluepairs = new arraylist<namevaluepair> ();
Delete.setentity (New Urlencodedformentity (Namevaluepairs));
HttpResponse response = Httpclient.execute (delete);
Enables Httpdelete to send body information in Java