android http设置body,android - HttpDelete with body - Stack Overflow

There are different interpretation in the question whether the body is allowed or not in the HTTP DELETE request. See this for example. In the HTTP 1.1 specification it is not explicitly prohibied. In my opinion you should not use body in the HTTP DELETE.

Nevertherless I think that you should use URL like mysite/myobject/objectId (shop.com/order/1234) where the objectId (a part of the url) is the additional information. As an alternative you can use URL parameters: mysite/myobject?objectName=table&color=red to send additipnal information to the server in the HTTP DELETE request. The part starting with '?' is the urlencoded parameters devided dy '&'.

If you want to send more complex information you can convert the data to JSON with respect of DataContractJsonSerializer or JavaScriptSerializer and then send the converted data (a string which I name myJsonData later) also as the parameter: mysite/myobject?objectInfo=myJsonData.

If you need to send too much additionnal data as a part of HTTP DELETE request so that you have problem with the URL length then you should probably better change the design of your application.

UPDATED: Iy you do want send some body per HTTP DELETE you can do this for example like following

// 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

the full code could be a little more complex, but this one already will work. Nevertheless I continue to say that Web Service needed data in the body of HTTP DELETE request is bad designed.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值