Enables Httpdelete to send body information in Java

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.