リクエストでボディが許可されているかどうかの問題には、さまざまな解釈がありますHTTP DELETE
。たとえば、これを参照してください。HTTP 1.1 仕様では、明示的に禁止されていません。私の意見では、 bodyで使用しないでくださいHTTP DELETE
。
mysite/myobject/objectId
とはいえ、 ( shop.com/order/1234
) のような URL を使うべきだと思いますが、 objectId
( url の一部) は付加情報です。別の方法として、URL パラメータを使用mysite/myobject?objectName=table&color=red
して、リクエストで追加情報をサーバーに送信することもできますHTTP DELETE
。「?」で始まる部分 '&' で区切られたurlencodedパラメータです。
より複雑な情報を送信する場合は、 DataContractJsonSerializerまたはJavaScriptSerializerに関してデータを JSON に変換し、変換されたデータ (後で名前を付ける文字列myJsonData
) をパラメーターとしても送信できますmysite/myobject?objectInfo=myJsonData
。
リクエストの一部としてあまりにも多くの追加データを送信する必要がありHTTP DELETE
、URL の長さに問題がある場合は、おそらくアプリケーションの設計を変更する必要があります。
更新: 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
完全なコードはもう少し複雑になる可能性がありますが、これは既に機能します。それにもかかわらず、HTTP DELETE リクエストの本文にある Web サービスの必要なデータは設計が悪いと言い続けます。