我使用DataContractJsonSerializer
并将StringContent
JSON 发送到 Web 服务:
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Employee));
MemoryStream ms = new MemoryStream();
serializer.WriteObject(ms, employee);
ms.Position = 0;
StreamReader sr = new StreamReader(ms);
StringContent jsonContent = new StringContent(sr.ReadToEnd(),
System.Text.Encoding.UTF8, "application/json");
// later I do HttpClient.PostAsync(uri, jsonContent)
这将产生此 Content-Type 标头:
Content-Type: application/json; charset=utf-8
是否可以省略字符集而只使用以下标题?
Content-Type: application/json
我没有看到这样做的过载StringContent
。