我正在使用以下代码在 RESTful Web 服务中发布(创建)一个对象。
JObject lead = new JObject();
lead.Add(new JProperty("FirstName", "John"));
lead.Add(new JProperty("LastName", "Doe"));
lead.Add(new JProperty("Company", "Acme"));
HttpWebRequest request = WebRequest.Create("https://na15.salesforce.com/services/data/v25.0/sobjects/Lead") as HttpWebRequest;
request.Method = "POST";
request.Accept = "application/json";
request.ContentType = "application/json";
request.Headers["Authorization"] = string.Format("Bearer {0}", "xxxxxx");
using (Stream requestStream = request.GetRequestStream())
{
using (StreamWriter writer = new StreamWriter(requestStream))
{
writer.Write(lead.ToString());
}
}
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
if (response.ContentLength > 0)
{
using (Stream stream = response.GetResponseStream())
{
// do something with response stream
}
}
}
以下是我在 Fiddler 中为请求和响应捕获的内容。
REQUEST:
POST https://na15.salesforce.com/services/data/v25.0/sobjects/Lead HTTP/1.1
Accept: application/json
Content-Type: application/json
Authorization: Bearer <<TOKEN>>
Host: na15.salesforce.com
Content-Length: 71
Expect: 100-continue
{
"FirstName": "John",
"LastName": "Doe",
"Company": "Acme"
}
RESPONSE:
HTTP/1.1 201 Created
Date: Mon, 21 Oct 2013 21:42:10 GMT
Sforce-Limit-Info: api-usage=19/5000
Location: /services/data/v25.0/sobjects/Lead/00Qi0000009RIFCEA4
Content-Type: application/json;charset=UTF-8
Content-Length: 54
{"id":"00Qi0000009RIFCEA4","success":true,"errors":[]}
但是,.NET 似乎将响应标头解释为以下内容,忽略 Content-Length 标头并将 Transfer-Encoding 标头设置为分块。
Sforce-Limit-Info: api-usage=21/5000
Transfer-Encoding: chunked
Content-Type: application/json;charset=UTF-8
Date: Mon, 21 Oct 2013 21:42:10 GMT
Location: /services/data/v25.0/sobjects/Lead/00Qi0000009RIFCEA4
为什么它会这样做,并且无论如何要覆盖这种行为?