在HTTP DELETE
请求中,对于身体是否被允许或不允许有不同的解释。例如,请参见this。在HTTP 1.1 specification它没有明确禁止。我认为你不应该在HTTP DELETE
中使用主体。
Nevertherless我认为你应该使用URL像mysite/myobject/objectId
(shop.com/order/1234
),其中objectId
(网址的一部分)是附加信息。作为替代,您可以使用URL参数:mysite/myobject?objectName=table&color=red
向HTTP DELETE
请求中的服务器发送附加信息。以'?'开头的部分是urlencoded参数,分为dy'&'。
如果你想发送更复杂的信息,您可以将数据转换成JSON与尊重DataContractJsonSerializer或JavaScriptSerializer然后发送转换后的数据(这是我的名字myJsonData
后的字符串),也为参数:mysite/myobject?objectInfo=myJsonData
。
如果您需要发送太多的附加数据作为HTTP DELETE
请求的一部分,以便您对URL长度有问题,那么您应该更好地更改应用程序的设计。
修订:IY你想发送每个HTTP一些身体DELETE比如,你可以做到这一点像下面
// somewhere above add: using System.Net; and using System.IO;
WebClient myWebClient = new WebClient();
// 1) version: do simple request
string t= myWebClient.UploadString ("http://www.examples.com/", "DELETE", "bla bla");
// will be send following:
//
// DELETE http://www.examples.com/ HTTP/1.1
// Host: www.examples.com
// Content-Length: 7
// Expect: 100-continue
// Connection: Keep-Alive
//
//bla bla
// 2) version do complex request
Stream stream = myWebClient.OpenWrite ("http://www.examples.com/", "DELETE");
string postData = "bla bla";
byte[] myDataAsBytes = Encoding.UTF8.GetBytes (postData);
stream.Write (myDataAsBytes, 0, myDataAsBytes.Length);
stream.Close(); // it send the data
// will be send following:
//
// DELETE http://www.examples.com/ HTTP/1.1
// Host: www.examples.com
// Content-Length: 7
// Expect: 100-continue
//
// bla bla
// 3) version
// create web request
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create ("http://www.examples.com/");
webRequest.Method = "DELETE";
webRequest.ServicePoint.Expect100Continue = false;
// post data
Stream requestStream = webRequest.GetRequestStream();
StreamWriter requestWriter = new StreamWriter (requestStream);
requestWriter.Write (postData);
requestWriter.Close();
//wait for server response
HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
// send following:
// DELETE http://www.examples.com/ HTTP/1.1
// Host: www.examples.com
// Content-Length: 7
// Connection: Keep-Alive
//
// bla bla
完整的代码可能是一个稍微复杂一些,但是这一次已经将工作。尽管如此,我仍然认为Web Service需要HTTP DELETE请求体中的数据是不好设计的。
你摇滚的家伙!谢谢! – 2013-05-22 03:32:52
呃!这似乎不适用于HttpURLConnection。我得到“java.net.ProtocolException:DELETE不支持写入”。 http://stackoverflow.com/questions/10338615/sending-http-delete-request-in-android – 2013-09-30 14:45:06
我与斯科特......我如何做到这一点与HttpURLConnection?我必须重写我的网络代码才能使用Apache HTTP? – 2014-03-03 16:37:09